gpt4 book ai didi

jsp - session 对象的getAttribute返回非空对象而不是Tomcat环境中JSP页面中的空对象

转载 作者:行者123 更新时间:2023-11-28 23:44:58 25 4
gpt4 key购买 nike

我在 Tomcat 环境中遇到 JSP session 对象的 getAttribute() 问题。

当从初始 jsp 页面 login.jsp 单击“连接”按钮时,将调用 connect.jsp 页面(连接页面)以获取数据库连接对象,并将该对象作为属性存储在 session 对象中。当用户点击登录页面的 Disconnect 按钮时,调用另一个页面 disconnect.jsp(断开页面),调用 getAttribute() 获取连接对象,并调用 setAttribute() 被调用以将连接对象设置为空。当使用 getAttribute() 检查连接对象的值时,它的值在断开页面中如预期的那样为 null。这符合预期。

断开连接后,在登录页面点击连接按钮再次连接数据库时,连接页面发现使用getAttribute()获取的连接对象不是空对象一个空对象。这就是问题。为什么 getAttribute() 返回一个非空对象,即使它已经在断开连接页面中设置为空?

代码大约有 10 次按预期工作!但大多数时候它都失败了。

这里是重现问题的实际代码:

登录页面:

   /* When connection page is invoked 2nd time after disconnecting an 
* existing connection, the connection page returns saying connection
* already exists! - this is not what I expect.
*
* The page invoke connection page if user clicks a connect button.
* It invokes disconnect page if user clicks a disconnect button.
*/


<HTML>
<BODY>
<FORM METHOD="POST" NAME="login_form">

<INPUT TYPE=SUBMIT VALUE="Connect" ONCLICK="react_connect(this.form)">
<INPUT TYPE=SUBMIT VALUE="Disconnect" ONCLICK="react_disconnect(this.form)">

<SCRIPT LANGUAGE="JavaScript">
function react_connect(form)
{
form.action = "connect.jsp";
form.submit();
}

function react_disconnect(form)
{
form.action = "disconnect.jsp";
form.submit();
}
</SCRIPT>

</FORM>
</BODY>
</HTML>

连接页面:

<HTML>
<BODY>
<FORM METHOD="POST" NAME="conn_form">

<%
HttpSession theSession = request.getSession(true);
String CONN_OBJ_NAME = "connobj";
// Use a string object instead of actual database connection object for
// simulating the problem.
String CONN_OBJ_VALUE = "dummy conn obj";

String conn = (String)theSession.getAttribute(CONN_OBJ_NAME);
if (conn == null) {
theSession.setAttribute(CONN_OBJ_NAME, CONN_OBJ_VALUE);
out.print("After connect: connobj=["+
theSession.getAttribute(CONN_OBJ_NAME).toString()+
"], sessionid="+theSession.getId());
} else {
out.print("Already connected. connobj=["+
theSession.getAttribute(CONN_OBJ_NAME).toString()+"], sessionid="+
theSession.getId());
}
%>

<BR><BR><BR>

<INPUT TYPE=SUBMIT VALUE="Back to Main page" ONCLICK="goback(this.form)">

<SCRIPT LANGUAGE="JavaScript">
function goback(form)
{
form.action = "login.jsp";
form.submit();
}
</SCRIPT>

</FORM>
</BODY>
</HTML>

断开页面:

<HTML>
<BODY>
<FORM METHOD="POST" NAME="disconn_form">
<%
HttpSession theSession = request.getSession(true);
String CONN_OBJ_NAME = "connobj";
String conn = (String)theSession.getAttribute(CONN_OBJ_NAME);
if (conn == null) {
out.print("Not connected to database.");
} else {
out.print("Before Disconnecting: connobj=[" +
theSession.getAttribute(CONN_OBJ_NAME).toString() +
"], sessionid="+theSession.getId());

// set connection object to null in the session.
theSession.setAttribute(CONN_OBJ_NAME, null);

out.print("<BR>After setting to null, connobj =[" +
theSession.getAttribute(CONN_OBJ_NAME)+"], sessionid="+
theSession.getId());

theSession.invalidate();
}
%>
<BR><BR><BR>

<INPUT TYPE=SUBMIT VALUE="Back to Main page" ONCLICK="goback(this.form)">

<SCRIPT LANGUAGE="JavaScript">
function goback(form)
{
form.action = "login.jsp";
form.submit();
}
</SCRIPT>

</FORM>
</BODY>
</HTML>

最佳答案

当您调用 getSession using boolean as 'true request.getSession(true); 时,如果未找到它会创建新 session 。

首先你应该在断开连接时使用getSession(false)获取 session 。
其次,使用 session.invalidate(); - 它使该 session 无效,然后解除绑定(bind)到它的任何对象。

Java Doc 引用 getSession(boolean create)

Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.

If create is false and the request has no valid HttpSession, this method returns null.

关于jsp - session 对象的getAttribute返回非空对象而不是Tomcat环境中JSP页面中的空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15506513/

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