public class Actions extends Action {
/*
* Generated Methods
*/ /**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionForms actionForms = (ActionForms) form;// TODO Auto-generated method stub
HttpSession session=request.getSession();
String type="";
type=(String)request.getParameter("type");
if("large".equals(type))
{
ArrayList arrayList=(ArrayList)BusImpl.busCheck();
request.setAttribute("busList", arrayList);
return mapping.findForward("large");
}
if("user".equals(type))
{
String userName=actionForms.getUserName();
String passWord=actionForms.getPassWord();
if(UserImpl.login(userName, passWord))
{
session.setAttribute("flag", "ok");
return mapping.findForward("check");
}
}
if("logout".equals(type))
{
if(session.getAttribute("flag")!=null)
{
session.invalidate();
}
}
if(session.getAttribute("flag")!=null)
{
int id=actionForms.getId();
if(id==0){}
else
{
BusImpl.busChange(id);
}
ArrayList arrayList=(ArrayList)BusImpl.busCheck();
request.setAttribute("busList", arrayList);
return mapping.findForward("check");
}
return mapping.findForward("large");
}
}以上是Action代码,注销以超链接“?”传type值为logout,但一注销就出下列错误:
javax.servlet.ServletException: java.lang.IllegalStateException: getAttribute: Session already invalidated
/*
* Generated Methods
*/ /**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionForms actionForms = (ActionForms) form;// TODO Auto-generated method stub
HttpSession session=request.getSession();
String type="";
type=(String)request.getParameter("type");
if("large".equals(type))
{
ArrayList arrayList=(ArrayList)BusImpl.busCheck();
request.setAttribute("busList", arrayList);
return mapping.findForward("large");
}
if("user".equals(type))
{
String userName=actionForms.getUserName();
String passWord=actionForms.getPassWord();
if(UserImpl.login(userName, passWord))
{
session.setAttribute("flag", "ok");
return mapping.findForward("check");
}
}
if("logout".equals(type))
{
if(session.getAttribute("flag")!=null)
{
session.invalidate();
}
}
if(session.getAttribute("flag")!=null)
{
int id=actionForms.getId();
if(id==0){}
else
{
BusImpl.busChange(id);
}
ArrayList arrayList=(ArrayList)BusImpl.busCheck();
request.setAttribute("busList", arrayList);
return mapping.findForward("check");
}
return mapping.findForward("large");
}
}以上是Action代码,注销以超链接“?”传type值为logout,但一注销就出下列错误:
javax.servlet.ServletException: java.lang.IllegalStateException: getAttribute: Session already invalidated
/*
* Generated Methods
*/ /**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionForms actionForms = (ActionForms) form;// TODO Auto-generated method stub
HttpSession session=request.getSession();
String type="";
type=(String)request.getParameter("type");
if("large".equals(type))
{
ArrayList arrayList=(ArrayList)BusImpl.busCheck();
request.setAttribute("busList", arrayList);
return mapping.findForward("large");
}
if("user".equals(type))
{
String userName=actionForms.getUserName();
String passWord=actionForms.getPassWord();
if(UserImpl.login(userName, passWord))
{
session.setAttribute("flag", "ok");
return mapping.findForward("check");
}
}
if("logout".equals(type))
{
if(session.getAttribute("flag")!=null)
{
session.invalidate();
}
}
if(session.getAttribute("flag")!=null)
{
int id=actionForms.getId();
if(id==0){}
else
{
BusImpl.busChange(id);
}
ArrayList arrayList=(ArrayList)BusImpl.busCheck();
request.setAttribute("busList", arrayList);
return mapping.findForward("check");
}
return mapping.findForward("large");
}
}
你 setAttribute 的时候没有放在 session 中,只是在当前的请求中
用 request.getSession.setAttribute();
就行了。
{
String userName=actionForms.getUserName();
String passWord=actionForms.getPassWord();
if(UserImpl.login(userName, passWord))
{
request.getSession().setAttribute("flag", "ok");
return mapping.findForward("check");
}
}
是改这里吗?
错误还出现,
{
if(session.getAttribute("flag")!=null)
{
session.invalidate();
}
} if(session.getAttribute("flag")!=null)
{
如果type等于logout的话就会注销,注销之后又执行下面的session.getAttribute("flag"),所以报错session已经失效
开头好像获取 session 了。
既然是getAttribute的时候失效,那可能就在这个周期前出问题了。
如果只是判断标志位的话而且用了struts,直接 request.setAttribute("flag", "ok");试试吧。
if("logout".equals(type))
{
if(session.getAttribute("flag")!=null)
{
request.getSession().setAttribute("flag", null);
}
}
多谢各位指点,又把这里改了就不出错了
没有意义的代码。即使'flag'在session中,你也可以setAttribute进行override
还有,要尽量控制session的memory使用哦