In this tutorial I am giving a very simple example which will demonstrate how you can create a login page, how can you authenticate them at the time of login, and finally logged out them in servlet.
@WebServlet Login and Logout Example
In this tutorial I am giving a very simple example which will demonstrate how you can create a login page, how can you authenticate them at the time of login, and finally logged out them in servlet.
Example :
login1.html
<!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>Login Page</title> </head> <body> <form method="get" action="RequestForwardExample"> <p>Give the following details</p> <table> <tr><td>Name </td> <td><input type="text" name="name"/></td></tr> <tr><td>Password </td> <td> <input type="password" name="psw"/></td></tr> <tr><td></td> <td><input type="submit" value="submit"/></td></tr> </table> </form> </body> </html>
RequestForwardExample.java
package roseindia.requestDispatcherExample; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet(name= "RequestForwardExample", urlPatterns= {"/RequestForwardExample"}) public class RequestForwardExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out= response.getWriter(); String name= request.getParameter("name"); String psw= request.getParameter("psw"); String className= "com.mysql.jdbc.Driver"; String url= "jdbc:mysql://192.168.10.13/data"; String user= "root"; String password= "root"; Connection con; PreparedStatement ps; ResultSet rs; try { Class.forName(className); con= DriverManager.getConnection(url, user, password); String sql= "SELECT name FROM login WHERE name = '"+name+"' AND password = '"+psw+"'"; ps= con.prepareStatement(sql); rs= ps.executeQuery(); if(rs.next()) { HttpSession session= request.getSession(); String n= rs.getString("name"); session.setAttribute("name", n); ServletContext context= getServletContext(); RequestDispatcher rd= context.getRequestDispatcher("/ForwardTesterServlet"); rd.forward(request, response); } else { ServletContext context= getServletContext(); RequestDispatcher rd= context.getRequestDispatcher("/login1.html"); out.println("<font color=red>invalid user name or password</font>"); rd.include(request, response); } con.close(); ps.close(); rs.close(); } catch(ClassNotFoundException cx) { out.println(); } catch(SQLException sx) { out.println(); } } }
ForwardTesterServlet.java
package roseindia.requestDispatcherExample; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("/ForwardTesterServlet") public class ForwardTesterServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out= response.getWriter(); HttpSession session= request.getSession(); String name= (String)session.getAttribute("name"); out.println("Welcome "+name); out.println("<h4>You are successfully logged in<h4>"); out.println("<html><body>"); out.println("<form method=get action= LogOutServlet>"); out.println("<input type=submit value=logout></input>"); out.println("</form></body></html>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
LogOutServlet.java
package roseindia.requestDispatcherExample; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("/LogOutServlet") public class LogOutServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out= response.getWriter(); HttpSession session= request.getSession(); session.invalidate(); out.println("<font color=red><b>You have been successfully logged out</b></font>"); out.println("<font color=green><b>Login again</b></font>"); ServletContext context= getServletContext(); RequestDispatcher rd= context.getRequestDispatcher("/login1.html"); rd.include(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Output :
When you will execute the above example you will get the output as :
When you will click on "submit" button you will get the output as :
But to logout when you will click on "logout" button you will get the output as :