This tutorial will teach you how to make a Automatic Logout after 30 minutes Inactivity using Jsp.When the user doesn’t log out the system that might be a problem. Any one can see the records of the system, it is not safe now a days every person leaves the mistakes when the works is busy. So that I did this system which help you in the future. If the user not access the system for 30 mins it will automatically log out.
First you have to create the login.jsp page and paste the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <style type="text/css"> body,td,th{ color: #000000; } body{ background-color: #F0F0F0; } .style1 { font-family: arial; font-size: 14px; padding: 12px; line-height: 25px; border-radius: 4px; text-decoration: none; } .style2 { font-family: arial; font-size: 17px; padding: 12px; line-height: 25px; border-radius: 4px; text-decoration: none; } </style> </head> <body> <div class="container"> <table width="100%" height="100%" border="0" cellpadding="0" align="center"> <tr> <td align="center" valign="middle"> <table class="table-bordered" width="350" border="0" cellpadding="3"cellspacing="3" bgcolor="#FFFFFF"> <Form name="frm-login" id="frm-login"> <tr> <td height="25" colspan="2" align="left" valign="middle" bgcolor="#FF9900" class="style2"> <div align="center"> <strong>Login</strong> </div> </td> </tr> <tr> <div id="err" style="color:red"> </div> </tr> <tr> <td width="118px" align="left" valign="middle" class="style1"> Username </td> <td width="118px" align="left" valign="middle" class="style1"> <input type="text" class="form-control" size="10px" id="username" placeholder="Username" name="username"> </td> </tr> <tr> <td width="118px" align="left" valign="middle" class="style1"> Password </td> <td width="118px" align="left" valign="middle" class="style1"> <input type="password" class="form-control" size="10px" id="password" placeholder="Password" name="password"> </td> </tr> <tr> <td colspan="2" valign="middle" class="style1" align="right"> <button type="button" class="btn btn-primary" onclick="login()">Sign In</button> <button type="button" class="btn btn-warning" onclick="reset()">Reset</button> </td> </tr> </Form> </table> </td> </tr> </table> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> var msg = null; function login() { if($('#username').val()== "") { $('#username').parent('td').addClass('has-error'); return false; } else if($('#password').val()== "") { $('#password').parent('td').addClass('has-error'); return false; } var data = $("#frm-login").serialize(); $.ajax({ type : 'POST', url : 'validatelogin', data : data, dataType : 'JSON', success:function(data) { msg = data[0].msg if(msg == 1) { window.location.replace("index.jsp"); } else if(msg == 3) { $("#err").hide().html("UserName or Password DO not Match").fadeIn('slow'); } } }); } </script> </body> </html> |
after that have to create the index.jsp page and paste the code
1 2 3 4 5 6 7 8 9 10 11 12 | <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1><%=session.getAttribute("username") %> </h1> <p><a href="logout.jsp">logout</a></p> </body> </html> |
after that have to create the logout.jsp page and paste the code
1 2 3 4 | <% session.invalidate(); response.sendRedirect(request.getContextPath() + "/login.jsp"); %> |
inside the source package have to create the validatelogin.java page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 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 java.util.logging.Level; import java.util.logging.Logger; 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; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @WebServlet("/validatelogin") public class validatelogin extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); Connection con; PreparedStatement pst; ResultSet rs; JSONArray list = new JSONArray(); String username = request.getParameter("username"); String password = request.getParameter("password"); JSONObject obj = new JSONObject(); try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/loginjsp", "root",""); pst = con.prepareStatement("select id,username,password from login where username = ? and password = ?"); pst.setString(1, username); pst.setString(2, password); rs = pst.executeQuery(); String msg; HttpSession session = request.getSession(); if(rs.next()) { session.setAttribute("username", username); session.setAttribute("password", password); session.setMaxInactiveInterval(30); msg = "1"; obj.put("msg", msg); list.add(obj); out.println(list.toJSONString()); out.flush(); } else { msg = "3"; obj.put("msg", msg); list.add(obj); out.println(list.toJSONString()); out.flush(); } } catch (ClassNotFoundException ex) { Logger.getLogger(validatelogin.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(validatelogin.class.getName()).log(Level.SEVERE, null, ex); } } } |
i have attached the video link below. which will do this tutorials step by step.