我在Tomcat环境中遇到JSP getAttribute()
会话对象的问题。
当从初始jsp页面login.jsp中单击Connect按钮时,将调用connect.jsp页面(连接页面)以获取数据库连接对象,并将该对象作为属性存储在会话对象中。当用户从登录页面单击“断开连接”按钮时,调用另一个页面disconnect.jsp(断开连接页面),调用getAttribute()
以获取连接对象,并调用setAttribute()
将连接对象设置为null 。使用getAttribute()
检查连接对象的值时,其值在断开连接页面中为null。这是预期的。
断开连接后,在登录页面上单击“连接”按钮以再次连接到数据库时,连接页面会发现使用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>
答案 0 :(得分:0)
当您使用getSession
作为'true boolean
调用request.getSession(true);
时,如果找不到,则会创建新的会话。
首先,您应该在断开连接时使用getSession(false)
进行会话
其次,使用session.invalidate();
- 它会使此会话无效,然后取消绑定绑定到它的任何对象。
getSession(boolean create)
返回与此请求关联的当前HttpSession,如果 没有当前会话且create为true,返回一个新会话。
如果create为false且请求没有有效的HttpSession,则为此 method返回null。