JSP 显示之前会话数据的页面

JSP Page showing previous session data

我在 jsp 中创建了一个登录表单。还制作了会话保存系统,当用户登录时,他的用户名将在标签标签内的欢迎页面中可见。我为此使用了这段代码 -

在登录页面中:

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

在欢迎页面中:

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

还有一个注销 link 像锚标记,它是 href="/login.jsp"

现在我来解决问题。当我第一次使用用户名“a”登录时,它在欢迎页面中成功显示“a”。但是当我注销并再次使用用户名“b”登录时,它也会在欢迎页面中显示“a”。当我刷新浏览器时,它会显示“b”。我可以尝试一种方法,我可以编写一段代码,在第一次刷新后刷新浏览器。但是我的页面将来会太重。为此,这不是标准方式,对吧?

我找到了一个 session.invalidate(); 的解决方案,但我在锚标记内尝试了这段代码,但没有用。

请帮忙!

这是注销页面代码-

<!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>

这是连接到表单方法的登录连接代码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);
    }

%>

问题是您首先 request 进入 /dashboard.jsp 页面,然后您创建 session 同时,您的标签显示先前创建的会话,因为您的页面 先加载再通过会话...

在 java 或任何其他程序中必须加载页面以获取新会话

使用 request.getRequestDispatcher() 而不是 getServletContext().getRequestDispatcherrs.isBeforeFirst() 不适用于 RowCount 使用 isBeforeFirst() 如果找到数据则选择第一行...

这里是修改后的代码:

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>