Authentication in webapplication using jsp and servlet is easy to do.
The Page that contains the forms are placed in jsp page and the action event is redirected to a servlet where authentication happens if authentication is success it redirect to authenticated page or else redirect to login page with message incorrect username/password.
Create a basic table in mysql or any database with two fields username and password and load the values for that.
Create a file login.jsp with code below and save in loginsystem folder in webapps of tomcat.
login.jsp


<html>
<head>
<script>
function checkNull(field)
{
	if(login.user.value == "")
	{
		alert("Enter the Login Name");
		login.user.focus();
		return false;
	}
	else if(login.pass.value == "" )
	{
		alert("Enter the Password");
		login.pass.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function focus()
{
	login.user.focus();
}
</script>
</head>
<body>

<form action ="login" method="post" name="login">

<p> </p>

<font color="#FF3300" size="6"> <a href="http://s2sgateway.com">Tutorial by S2SGATEWAY.COM</a>!!!!!!!!!</font></p>

<a title="Press To Go Back" href="login.jsp" style="text-decoration: none">
<font color="#FFFFFF" face="Bauhaus 93" size="4"><-- BACK</font></a></p>
<div align="center">
  <center>
<div align="center">
  <center>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#0099cc" width="51%" id="AutoNumber1" bgcolor="#0099CC" height="134">
  <tr>
    <td width="100%" colspan="3" height="19">
      <p align="center">
      <b><font face="Verdana" color="#00FF00">ADMIN LOGIN 
      </font></b></p>

    </td>
  </tr>
  <tr>
    <td width="30%" height="23"><font face="Verdana" size="2" color="#800080">
    USER NAME</font></td>
    <td width="72%" colspan="2" height="23"><font color="#800080"><input type="text" name="user" size="20" onload = 'T1.focus();'></font></td>
  </tr>
  <script>
	focus();
  </script>

  <tr>
    <td width="30%" height="23"><font face="Verdana" size="2" color="#800080">
    PASSWORD</font></td>
    <td width="72%" colspan="2" height="23"><font color="#800080"><input type="password" name="pass" size="20">   
    </font> </td>
  </tr>

  <tr>
    <td width="30%" height="23"> </td>
    <td width="72%" colspan="2" height="23"> </td>
  </tr>
  <tr>
    <td width="30%" height="26"> </td>
    <td width="18%" height="26">
    <p align="left"><font color="#800080"><input type="submit" value="Login" name="B1" onclick = 'return checkNull();'></font></td>
    <td width="54%" height="26"><font color="#800080"><input type="reset" value="Reset" name="B2"></font></td>
  </tr>
</table>

  </center>
</div>
<p align="left"><font color="#008080">          </font></p>

<p align="left"><font color="#008080">                                                                        
</font></p>

<p align="left"><font color="#008080">                  
</font></p>

  </center>
</div>
</form>
</body>
</html>

Now create a java file named login.java with code below and compile it and place the class in web-inf/classes folder.
login.java



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.*;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class login extends HttpServlet {

	public login() {
		super();
	}

	public void destroy() {
		super.destroy(); 

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String a=request.getParameter("user");
String b=request.getParameter("pass");
HttpSession s=request.getSession();
login l =(login)s.getAttribute("a");

System.out.println(a);
	try
		{
			Class.forName("com.mysql.jdbc.Driver");

			Connection con=DriverManager.getConnection("jdbc:mysql://localhost/dbname","username","password");

			Statement st=null;
			String c="select * from login where username='"+a+"' AND pass='"+b+"'";
			st=con.createStatement();
			ResultSet rs=st.executeQuery(c);

			if(rs.next())
			{
				response.sendRedirect("success.jsp");

			}
			else
			response.sendRedirect("login.jsp");

		}
	catch(Exception e){}
	}

	public void init() throws ServletException {

	}

}

Now add the servlet setting in web.xml like below

web.xml


<wep-app>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>login</servlet-name>
    <servlet-class>login</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>home.html</welcome-file>
  </welcome-file-list>
</web-app>

Thats it now you can run by visiting your tomcat http://localhost:8080/loginsystem/login.jsp
Any doubt ask us happy coding

Related Posts

captcha project in jsp/servlet, How to run servlet in tomcat 5.5, HTTP Status 404 – Servlet as is not available, Creating captcha(random letter image) in jsp, Display combo box value/Database value on same JSP page