gpt4 book ai didi

java - 显示先前 session 数据的 JSP 页面

转载 作者:行者123 更新时间:2023-12-05 05:55:40 29 4
gpt4 key购买 nike

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

在登录页面:

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

欢迎页面:

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

还有一个注销链接,例如 href="/login.jsp"

的 anchor 标记

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

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

请帮忙!

这是注销页面代码-

<!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);
}

%>

最佳答案

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

在 java 或任何其他编程中必须加载页面才能获取新 session

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

修改后的代码:

登录.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>

仪表板.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>

关于java - 显示先前 session 数据的 JSP 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69413508/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com