我希望在网页中处理重登陆问题,希望先去除原来的session,在新建session,采用书上介绍的方法:
HttpSession oldSession = request.getSession(false);
if(oldSession != null) {
oldSession.invalidate();
}
session = request.getSession(true);
结果却出现异常,提示session已被invalidate,
请问有什么办法可以处理这个问题呢?
给你我们的处理方法:
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.io.*,java.util.*,javax.servlet.http.*"%>
<%
try{
//取得系统的Session值
String staff_id=(String) session.getAttribute("staff_id"); //操作员ID
//清楚页面的session
String strName="";
Enumeration eList=(Enumeration)session.getAttributeNames();
Vector vObj=new Vector();
for (; eList.hasMoreElements() ;) {
String name=new String((String)eList.nextElement());
vObj.addElement(name);
}
String[] arrName=new String[vObj.size()];
vObj.copyInto(arrName);
for (int i=0;i<arrName.length;i++) {
session.removeAttribute(arrName[i]);
}
//重新取得系统Session
session.setAttribute("staff_id",staff_id);
}catch(Exception e){
System.out.println("Error clear session!");
e.printStackTrace();
}