Java清除session的方法

李鹏
2023-12-01
看这篇文章前你可以看看:  session的关闭的机制  

第一种方法(继承SessionAware类来取得session,然后用invalidate()方法清理)
  1. public class ExitAction extends ActionSupport implements SessionAware{
  2. @Override
  3. public String execute() throws Exception {
  4. HttpServletRequest request = ServletActionContext.getRequest();
  5. HttpSession session1 = request.getSession();
  6. session1.invalidate();
  7. return super.execute();
  8. }

  9. public void setSession(Map arg0) {


  10. }

  11. }

//第二种方法(用ActionContext取session,然后用clear()方法清理)

  1. public class ExitAction extends ActionSupport{

  2. @Override
  3. public String execute() throws Exception {
  4. ActionContext ac = ActionContext.getContext();
  5. Map session = ac.getSession();
  6. session.remove("buser");
  7. session.remove("guser");
  8. session.remove("fuser");
  9. return super.execute();
  10. }

  11. }
 类似资料: