Simple login example using Servlet and JSP


            This example shows how to create a sample login page using servlet and JSP to validate username and password.

 

Example

Login page

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Sample login Example (try with username as "admin" and password as "admin" without quart ) <br> <br>

<form action="LoginController" method="post">

Enter username :<input type="text" name="username"> <br>
Enter password :<input type="password" name="password"><br>
<input type="submit" value="Login">


</form>

</body>
</html>

Login controller

package com.candidjava;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginController
 */
public class LoginController extends HttpServlet {
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String un=request.getParameter("username");
		String pw=request.getParameter("password");
		
		if(un.equals("admin") && pw.equals("admin"))
		{
			response.sendRedirect("success.html");
			return;
		}
		else
		{
			response.sendRedirect("error.html");
			return;
		}
	}

}

success page

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


Login success

</body>
</html>

error page

 <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Invalid username or password
</body>
</html>

Screenshot

             

            

            


Download

                Login example war

                Login example zip



Comments

manvendra pratap dubey
         i want code for dynamic data validation...u have done it for static data...please help with that and also for registration page. Thanks in advance
sudheer
         please tell me external css folder witch please inserted this project and how call the jsp page in css
sunish
         thanks so much may god bless u
Pradeep
         Thank you.
CodingNow
         are you stuck in '90s ?? That's awful

©candidjava.com