In this article, we’re going to look at a basic login verification form using JSP and servlet. If a user’s entered username and password matches with original username and password then ‘welcome’ page would open.
Here’s the code with two textboxes username and password on receiving the request to verify the user details and display welcome username on another page.
1.Make a landing page in JSP where two text boxes and a submit button will be displayed.
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Practical Work</title> </head> <body> <% String msg = (String) request.getAttribute("message"); if(msg==null) out.println("\t"); else out.println(msg); %> <form action="login"> <label>Username : </label> <input type="text" name="username"/> <label>Password : </label> <input type="password" name="pass"/> <button type="submit">Login</button><br/> </form> </body> </html>
2. Now, it’s time to verify the user which would be done by servlet at backend.
login.java
package com.programmerbay; import java.io.IOException; import java.io.PrintWriter; 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.servlet.http.HttpSession; public class login extends HttpServlet{ public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { String username = request.getParameter("username") ; String password = request.getParameter("pass") ; if(username.equals("programmerbay") && password.equals("12345")) { HttpSession session = request.getSession(); session.setAttribute("username", username); response.sendRedirect("home.jsp"); } else { String msg; if(username.isEmpty() || password.isEmpty()) msg ="Fields are empty"; else msg ="Password is incorrect!!"; request.setAttribute("message", msg); RequestDispatcher redirect = request.getRequestDispatcher("index.jsp"); redirect.forward(request,response); } } }
3. If a user type in correct username and password, ‘welcome’ page would be displayed.
Home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Home</title> </head> <body> <% String username = (String)session.getAttribute("username"); if(username==null) { response.sendRedirect("index.html"); } %> <h1>This is home Screen</h1> <form action="logout" action="get"> <button type="submit">Logout</button> </form> </body> </html>
4. To end the session, a logout servlet will work whenever a user clicks on the logout button.
logout.java
package com.programmerbay; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class logout extends HttpServlet{ public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { HttpSession session = request.getSession(); session.removeAttribute("username"); session.invalidate(); response.sendRedirect("index.jsp"); } }
Output:
Login page
When either username or password is not entered
When a user enters a wrong password or username
Homepage