login through jsp and servlet.
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="newcss.css" media="screen" type="text/css" />
<head>
<meta charset="utf-8">
<title>Login</title>
<script type="text/javascript">
function check()
{
var a=document.getElementById("name").value;
var b=document.getElementById("pass").value;
if(a=="")
{
alert("Enter your username!!");
return false;
}
else if(a.length<5)
{
alert("Pls fill correct username!!");
}
if(b=="")
{
alert("Enter your password!!");
return false;
}
return true;
}
</script>
</head>
<body>
<center> <h1>Inventory Management System</h1></center>
<center><div><img src="images.jpg"/></div></center>
<div id="login">
<form action="Loginact" method="post" onsubmit="return check();">
<fieldset class="clearfix">
<p><input type="text" id="name" name="name" placeholder="username"></p> <!-- JS because of IE support; better: placeholder="Username" -->
<p><input type="password" id="pass" name="pass" placeholder="password"></p> <!-- JS because of IE support; better: placeholder="Password" -->
<p><input type="submit" value="Sign In" ></p>
</fieldset>
</form>
<p>Not a member? <a href="#">Sign up now</a><span class="fontawesome-arrow-right"></span></p>
</div> <!-- end login -->
</body>
</html>
Loginact.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Connect;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
/**
*
* @author NAVJYOTI
*/
public class Loginact extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try
{
PrintWriter out=response.getWriter();
databse d=new databse();
Connection con=d.connectit();
String sql="select * from register where name='"+request.getParameter("name")+"' and password='"+request.getParameter("pass")+"'";
PreparedStatement pst=con.prepareStatement(sql);
out.print(pst);
ResultSet rs=pst.executeQuery();
int count = 0;
while(rs.next())
{
count = count + 1;
}
if(count == 1)
{
out.print("<script type='text/javascript'>alert('access granted!!');</script>");
//out.print(request.getParameter("name"));
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.forward(request, response);
}
else if(count > 1){
out.print("<script type='text/javascript'>alert('Duplicate User!!');</script>");
//out.print(request.getParameter("pass"));
RequestDispatcher rd=request.getRequestDispatcher("login");
rd.forward(request, response);
}
else{
out.print("<script type='text/javascript'>alert('User not Found!!');</script>");
response.sendRedirect("login");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
<html>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="newcss.css" media="screen" type="text/css" />
<head>
<meta charset="utf-8">
<title>Login</title>
<script type="text/javascript">
function check()
{
var a=document.getElementById("name").value;
var b=document.getElementById("pass").value;
if(a=="")
{
alert("Enter your username!!");
return false;
}
else if(a.length<5)
{
alert("Pls fill correct username!!");
}
if(b=="")
{
alert("Enter your password!!");
return false;
}
return true;
}
</script>
</head>
<body>
<center> <h1>Inventory Management System</h1></center>
<center><div><img src="images.jpg"/></div></center>
<div id="login">
<form action="Loginact" method="post" onsubmit="return check();">
<fieldset class="clearfix">
<p><input type="text" id="name" name="name" placeholder="username"></p> <!-- JS because of IE support; better: placeholder="Username" -->
<p><input type="password" id="pass" name="pass" placeholder="password"></p> <!-- JS because of IE support; better: placeholder="Password" -->
<p><input type="submit" value="Sign In" ></p>
</fieldset>
</form>
<p>Not a member? <a href="#">Sign up now</a><span class="fontawesome-arrow-right"></span></p>
</div> <!-- end login -->
</body>
</html>
Loginact.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Connect;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
/**
*
* @author NAVJYOTI
*/
public class Loginact extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try
{
PrintWriter out=response.getWriter();
databse d=new databse();
Connection con=d.connectit();
String sql="select * from register where name='"+request.getParameter("name")+"' and password='"+request.getParameter("pass")+"'";
PreparedStatement pst=con.prepareStatement(sql);
out.print(pst);
ResultSet rs=pst.executeQuery();
int count = 0;
while(rs.next())
{
count = count + 1;
}
if(count == 1)
{
out.print("<script type='text/javascript'>alert('access granted!!');</script>");
//out.print(request.getParameter("name"));
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.forward(request, response);
}
else if(count > 1){
out.print("<script type='text/javascript'>alert('Duplicate User!!');</script>");
//out.print(request.getParameter("pass"));
RequestDispatcher rd=request.getRequestDispatcher("login");
rd.forward(request, response);
}
else{
out.print("<script type='text/javascript'>alert('User not Found!!');</script>");
response.sendRedirect("login");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}