Home > Mobile >  JSP Page showing previous session data
JSP Page showing previous session data

Time:10-14

I created a login form in jsp. Also made the session save system that when the user will log in his username will be visible in the welcome page inside a label tag. I used this code for this -

In the login page :

request.getSession().setAttribute("username",username);

And in the welcome page :

out.print(request.getSession().getAttribute("username"));

Also have an log out link like anchor tag which's href="/login.jsp"

Now I comes to the problem. When I first time login with the username "a" it successfully shows "a" in the welcome page. But when I logout and login again with username "b" then also it shows "a" in the welcome page. When I refresh the browser then it shows "b". I can try a method that I can make a code that will refresh the browser after first refresh. But my page will be too heavy in future. For that this will be not a standard way right?

I found a solution that session.invalidate(); but I tried this code inside anchor tag but is of no use.

Kindly help!

Here is the logout page code -

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript" src="js/basic.js"></script>
    <title>Dashboard || XCEL-ERP</title>
</head>
<body>
    <div class="dashboard_sidebar" id="dashboard_sidebar">
        <ul>
            <li class="sidebar_item"><a href="#">HOME</a></li>
            <li class="sidebar_item"><a href="#">RECIPE & COSTING</a></li>
            <li class="sidebar_item"><a href="#">PLANNING</a></li>
            <li class="sidebar_item"><a href="#">PRODUCTION</a></li>
            <li class="sidebar_item"><a href="#">ACCOUNT</a></li>
            <li class="sidebar_item"><a href="#">OVERVIEW</a></li>
        </ul>
    </div>
    <div class="dashboard_header" id="dashboard_header">
        <div class="dashboard_hamburger" onclick="sidebarFunction()">
            <span></span>
            <span></span>
            <span></span>
        </div>
        <div class="logo">ERP</div>
        <div class="user">
            <img src="https://www.dropbox.com/s/s3wcx1zot0gp3fe/user_icon.png?raw=1" alt="user" onclick="userProfileFunction()">
            <label>
             <%  

                out.print(request.getSession().getAttribute("username"));

             %>
             </label>
             <div class="userProfileBox" id="userProfileBox">
                <img src="https://www.dropbox.com/s/zq3d2dz63y3y4ct/man_pic.jpg?raw=1" alt="Profile_img">
                <div class="userName"> <% out.print(request.getSession().getAttribute("username")); %> </div>
                <div class="desg_Section"> Costing Officer - Wash </div>
                <div class="authorization">User has authorization of : </div>
                <button>Log Out</button>
             </div>
        </div>
        <a href="login.jsp">Log Out</a>
    </div>
</body>
</html>

And here is the login connection code which is connected to a form method post -

<%@ page import = "java.sql.*"%>
<%

    String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    String DB_URL = "jdbc:mysql://localhost:3306/erp";
    String DB_USER = "root";
    String DB_PASS = "";

    Connection conn = null;
    Statement stm = null;
    ResultSet rs = null;

    String username = request.getParameter("username");
    String password = request.getParameter("password");

    String query = "SELECT * FROM userdata WHERE UserName = '".concat(username).concat("' AND Password = '").concat(password).concat("'");

    try{
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
        stm = conn.createStatement();

        rs = stm.executeQuery(query);

        Boolean match_result = rs.isBeforeFirst();

        if(match_result == true){

            getServletContext().getRequestDispatcher("/dashboard.jsp").forward(request, response);

            
            request.getSession().setAttribute("username",username);

        }else{

            out.print("Sorry, username or password error!");  
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            
        }

        rs.close();
        stm.close();
        conn.close();
    } catch(Exception e){
        out.println(e);
    }

%>

CodePudding user response:

The problem is first you request for going /dashboard.jsp page then you create session meanwhile, your label show previously created session because your page first load and then pass session...

it is mandatory to load page for get new session in java or any other programming

Use request.getRequestDispatcher() instead of getServletContext().getRequestDispatcher. rs.isBeforeFirst() is not for RowCount use of isBeforeFirst() is if data found pick first row...

here is modified code:

login.jsp

<%@ page import = "java.sql.*"%>
<%

    String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    String DB_URL = "jdbc:mysql://localhost:3306/erp";
    String DB_USER = "root";
    String DB_PASS = "";

    Connection conn = null;
    ResultSet rs = null;

    if (request.getParameter("btnLogin") != null) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
            String query = "SELECT * FROM userdata WHERE UserName = '".concat(username).concat("' AND Password = '").concat(password).concat("'");
            PreparedStatement pst = conn.prepareStatement(query);
            rs = pst.executeQuery();
            if (rs.next()) {
                request.getSession().setAttribute("username", username);
                request.getRequestDispatcher("/dashboard.jsp").forward(request, response);
            } else {
                out.print("Sorry, username or password error!");
                request.getRequestDispatcher("/login.jsp").forward(request, response);

            }

            pst.close();
            conn.close();
        } catch (Exception e) {
            out.println(e);
        }
    }

%>

<form method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" name="btnLogin" value="submit">
</form>

dashboard.jsp

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript" src="js/basic.js"></script>
    <title>Dashboard || XCEL-ERP</title>
</head>
<body>
    <div class="dashboard_sidebar" id="dashboard_sidebar">
        <ul>
            <li class="sidebar_item"><a href="#">HOME</a></li>
            <li class="sidebar_item"><a href="#">RECIPE & COSTING</a></li>
            <li class="sidebar_item"><a href="#">PLANNING</a></li>
            <li class="sidebar_item"><a href="#">PRODUCTION</a></li>
            <li class="sidebar_item"><a href="#">ACCOUNT</a></li>
            <li class="sidebar_item"><a href="#">OVERVIEW</a></li>
        </ul>
    </div>
    <div class="dashboard_header" id="dashboard_header">
        <div class="dashboard_hamburger" onclick="sidebarFunction()">
            <span></span>
            <span></span>
            <span></span>
        </div>
        <div class="logo">ERP</div>
        <div class="user">
            <img src="https://www.dropbox.com/s/s3wcx1zot0gp3fe/user_icon.png?raw=1" alt="user" onclick="userProfileFunction()">
            <label>
             <%  

                out.print(request.getSession().getAttribute("username"));

             %>
             </label>
             <div class="userProfileBox" id="userProfileBox">
                <img src="https://www.dropbox.com/s/zq3d2dz63y3y4ct/man_pic.jpg?raw=1" alt="Profile_img">
                <div class="userName"> <% out.print(request.getSession().getAttribute("username")); %> </div>
                <div class="desg_Section"> Costing Officer - Wash </div>
                <div class="authorization">User has authorization of : </div>
             </div>
        </div>
        <a href="RemoveSession.jsp">Log Out</a>
    </div>
</body>
</html>

RemoveSession.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="post">
            <%
                session.invalidate();
                request.getRequestDispatcher("/login.jsp").forward(request, response);
                
            %>
        </form>
    </body>
</html>
  • Related