getAttribute()
Tomcat 環境で JSP のセッション オブジェクトに問題があります。
最初の jsp ページ login.jsp から [接続] ボタンをクリックすると、connect.jsp ページ (接続ページ) が呼び出されてデータベース接続オブジェクトが取得され、オブジェクトが属性としてセッション オブジェクトに格納されます。ユーザーがログイン ページから [切断] ボタンをクリックすると、別のページ disconnect.jsp (切断ページ) が呼び出され、getAttribute()
接続オブジェクトを取得するために呼び出さsetAttribute()
れ、接続オブジェクトを null に設定するために呼び出されます。を使用して接続オブジェクトの値をチェックするとgetAttribute()
、その値は切断ページで予想されるように null です。これは予想通りです。
切断後、ログイン ページで [接続] ボタンをクリックしてデータベースに再度接続すると、接続ページgetAttribute()
で、null オブジェクトではなく、null 以外のオブジェクトを使用して取得された接続オブジェクトが検出されます。これが問題です。getAttribute()
切断ページで既に null に設定されているにもかかわらず、null 以外のオブジェクトを返すのはなぜですか?
おそらく 10 回に 1 回は、コードが期待どおりに機能します。しかし、ほとんどの場合、失敗します。
問題を再現するための実際のコードは次のとおりです。
ログインページ:
/* 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>