在Tomcat环境中的JSP页面中,会话对象的getAttribute返回非空对象,而不是空对象



我在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>

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

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

从Java Doc中引用getSession(boolean create)

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

如果create为false,并且请求没有有效的HttpSession,则会触发此错误方法返回null。

最新更新