【问题标题】:getAttribute of session object returns non-null object instead of null object in a JSP page in Tomcat environmentTomcat环境下JSP页面中session对象的getAttribute返回非空对象而不是空对象
【发布时间】:2013-03-19 17:32:33
【问题描述】:

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

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

断开连接后,在登录页面点击Connect按钮再次连接数据库时,连接页面发现使用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>

【问题讨论】:

    标签: jsp session tomcat getattribute


    【解决方案1】:

    当您使用boolean 作为'true request.getSession(true); 调用getSession 时,如果找不到,它会创建新会话。

    首先,您应该在断开连接时使用getSession(false) 获得会话。
    其次,使用session.invalidate(); - 它使这个会话无效,然后解除绑定到它的所有对象。

    Java Doc引用getSession(boolean create)

    返回与此请求关联的当前 HttpSession,或者,如果 没有当前会话并且 create 为真,返回一个新会话。

    如果 create 为 false 并且请求没有有效的 HttpSession,这 方法返回 null。

    【讨论】:

    • 我尝试了您的建议,但没有成功。我将使用简单的 jsp 脚本更新我的帖子以重现问题。
    • 我发布的修改后的代码在 MS Internet Explorer 9.0 上失败了,但我现在发现它在谷歌 Chrome 浏览器上运行得很好!
    • @user2178786:如果有帮助,您应该接受答案。这对您的个人资料也有好处。
    • 虽然您建议的更改并没有解决我在 Internet Explorer 上遇到的问题,但我相信这些更改适合我的应用程序并且我会使用它们。谢谢你,感谢你的努力和建议。
    猜你喜欢
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    相关资源
    最近更新 更多