Jsp

Automatic Logout Inactivity using Jsp Servlet

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 systemit is not safe now a days every person leaves the mistakes when the works is busySo that I did this system which help you in the futureIf 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

<%@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" >

after that have to create the index.jsp page and paste the code

<%@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

<%
    session.invalidate(); 
    response.sendRedirect(request.getContextPath() + "/login.jsp");
%>

inside the source package have to create the  validatelogin.java page

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.

 

 

 

admin

Recent Posts

ChatGPT Free Online Download the ChatGPT App Easily

Have you ever wanted to talk to a friendly, smart robot that helps you with…

6 days ago

Free GPT Chat? DeepSeek AI Does It Better

DeepSeek AI is becoming a friendly and powerful new tool in the world of artificial…

2 weeks ago

Spring Boot MySQL Complete CRUD REST API [ Free Sourecode ]

Do you want to become an expert in Spring Boot CRUD operations? This comprehensive tutorial…

3 weeks ago

CREATE Responsive Navigation Bar with FlexBox CSS!

Modern websites must have a navigation bar that is clear and responsive. FlexBox CSS is…

4 weeks ago

Registration with image upload Java Jdbc(Download Source code)

Introduction In this section, we will guide you step by step in the development of an image upload registration system in Java using MySQL and JDBC. In the application, users register…

2 months ago

Touchable shop Pos system using Java

The Touchable Shop POS (Point of Sale) system is a sophisticated software solution developed using…

3 months ago