gpt4 book ai didi

java - 无法获取 session 属性(新 session 正在 JSP 中创建)为什么?

转载 作者:行者123 更新时间:2023-11-28 03:13:51 27 4
gpt4 key购买 nike

登录.jsp

    <%
/* Getting Database connection*/
Connection connection = ConnectionFactory.getInstance()
.getConnection();
if (connection == null) {
System.out.print("Error in getting DB connection");
}

/* requesting user login credentials*/
String login_name = request.getParameter("username");
String login_pass = request.getParameter("password");

if ((login_name != null && login_name.trim() != "")
|| (login_pass != null && login_pass.trim() != "")) {

String query = "SELECT USER_NAME,PASSWORD from MEMBERS where USER_NAME='"
+ login_name + "'";
PreparedStatement ps = connection.prepareStatement(query);
ResultSet rs = ps.executeQuery();
String USER_NAME = null;
String PASSWORD = null;
while (rs.next()) {
USER_NAME = rs.getString("USER_NAME");
PASSWORD = rs.getString("PASSWORD");
}

//verifying with database
if ((USER_NAME.equals(login_name) && PASSWORD
.equals(login_pass))) {
//starting session
session.setAttribute("UserName", USER_NAME);

System.out.println(session.getCreationTime());
out.print("<head> <meta http-equiv=\"Refresh\" content=\"0;url=./home.jsp\" > </head>");

} else {
out.println("Login Failed");
}
} else {
out.println("Please Enter Your Credentials");
%>
<jsp:include page="./login.html"></jsp:include>
<%

}
%>

主页.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
Hi, <%= session.getAttribute("UserName") %>


<% System.out.println(session.getCreationTime());%>
</body>
</html>

Home.jsp 显示 Hi,null 为什么?我知道 home .jsp 正在创建新 session ,但如何利用现有 session ?任何机构都可以帮助解决这个问题吗?

最佳答案

尝试将此声明添加到您的页面:

<%@ page session="false" %>

这将防止在提供 JSP 时创建 session 。此外,我认为这与 session 无关,而与正在设置的 USER_NAME 的值有关。尝试调试在 login.jsp 中设置的 USER_NAME 的值

关于java - 无法获取 session 属性(新 session 正在 JSP 中创建)为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29249431/

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