我getAttribute()
在 Tomcat 环境中遇到了 JSP 会话对象的问题。
当从初始 jsp 页面 login.jsp 中单击 Connect 按钮时,将调用 connect.jsp 页面(连接页面)以获取数据库连接对象,并将该对象作为属性存储在会话对象中。当用户从登录页面点击断开按钮时,另一个页面disconnect.jsp(断开页面)被调用,getAttribute()
被调用以获取连接对象,并被setAttribute()
调用以将连接对象设置为空。当使用 来检查连接对象的值时getAttribute()
,它的值为 null,正如断开页面中所预期的那样。这正如预期的那样。
断开连接后,在登录页面点击Connect按钮再次连接数据库时,连接页面发现使用的连接对象getAttribute()
是非空对象,而不是空对象。这就是问题。getAttribute()
即使在断开连接页面中已将其设置为 null,为什么还要返回非 null 对象?
大约有 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>