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


 Related example

Jsp servlet login example

Registration form using servlet      

Login session management in servlet using cookie  

Login session management in servlet using HttpSession

File upload using jsp and servlet                         

File download using jsp and servlet





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.
navyakantha
         good
navyakantha
         i want more data
anil
         how to execute this in eclips
Anand vardhan reddy
         could you let me know how to create envirement for simple login and save the data in database....
Candidjava
         Sure Anand vardhan reddy, will update you the code in couple of days

©candidjava.com