浅析清除session的几种方法

来源:互联网 发布:呼死你淘宝怎么找 编辑:程序博客网 时间:2024/06/06 08:24

第一种方法(继承SessionAware类来取得session,然后用invalidate()方法清理)

session.removeAttribute("sessionname")是清除SESSION里的某个属性.  
  session.invalidate()是让SESSION失效.  
  或许你可以用getAttributeNames来得到所有属性名,然后再removeAttribute  

我是如下在Spring中实現的:

 private void initSession(HttpServletRequest request){
  Enumeration em = request.getSession().getAttributeNames();
  while(em.hasMoreElements()){
   request.getSession().removeAttribute(em.nextElement().toString());
  }
 }

  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. }
0 0
原创粉丝点击