同一个jsp文件,有两次调用session.getAttribute( "id "),第一次正常得到对应值
第二次调用竟抛出异常,提示getAttribute:Session already invalidated
无奈我就加了这句session.setMaxInactiveInterval(-1);
仍然出错。。
为什么啊?
=========源文件==========
<%@ page language= "java " contentType= "text/html; charset=gb2312 "
pageEncoding= "gb2312 "%>
<%@ page session= "true " %>
<%@ page import= "java.util.*,java.sql.*,java.util.Date "%>
<% response.setCharacterEncoding( "gb2312 ");
// 禁止浏览器缓冲保留数据
response.setHeader( "Cache-Control ", "no-store ");
response.setHeader( "Pragma ", "no-cache ");
response.setDateHeader( "Expores ",0);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> test </title>
</head>
<body>
<%@include file= "head.html "%>
<jsp:useBean id= "DBconn " scope= "page " class= "zhj.web.DBconnect "/>
<%session.setMaxInactiveInterval(-1);
out.println(session);
Date d = new Date(session.getCreationTime());
Calendar cal = Calendar.getInstance();
cal.setTime(d);
out.println(new java.text.SimpleDateFormat( "hh:mm:ss ").format(d));
// out.println(cal.get(Calendar.HOUR)+ ": "+cal.get(Calendar.MINUTE)+ ": "+cal.get(Calendar.SECOND));
.......//此处省略的为数据库操作
<%out.println(session.getAttribute( "id "));%> // 这里调用正常
if(rs2.next()){
user=rs2.getString(2);
rs2.close();
session.setAttribute( "id ",userId); // 在这里添加到session
msg = "恭喜!!登录成功 ";
}else msg = "对不起,会员号或密码有误,您可以重新登录 ";
.....//此处省略的为显示数据库数据
<%try{ // 下面的调用异常
out.println(session.getAttribute( "id "));
}catch(Exception e){
out.println(e.getMessage());
}
%>
<%@include file= "tail.html "%>
</body>
</html>
------解决方法--------------------------------------------------------
user=rs2.getString(2);
rs2.close();
session.setAttribute( "id ",userId); // 在这里添加到session
msg = "恭喜!!登录成功 ";
这个 userid 是从哪来的?
------解决方法--------------------------------------------------------
确定是那个位置出错吗? 你选备份原文件后, 把贴子中你略去的部分从jsp中删除调试下.
------解决方法--------------------------------------------------------
不用取两次,你第二次用完全可以用第一次取出来的变量userid