struts2中設置session及其有效期時間以及清除session實現退出登錄

                    ---------------------------------------------參考博客資料整理-------------------------------------------------

1. 設置session  【參考http://blog.csdn.net/maxracer/article/details/5357458】


        ServletActionContext.getRequest().getSession().setAttribute("user", u);


       ServletActionContext.getRequest()得到Request請求,然後getSession()得到Session,最後使用setAttribute("user", u)往session

       中放入名爲user的變量,u是user的值

      ActionContext.getContext().getSession().put(key,value); 也可以實現

2. action中設置session有效期的三種方法  【參考http://omyyal.iteye.com/blog/1728610】

    方法一:
         在使用了struts2框架的任何地方使用
         ServletActionContext.getRequest().getSession().setMaxInactiveInterval(xxx);

    方法二:
         在Action中定義一個HttpServletRequest的成員對象req
        Action類實現ServletRequestAware接口,實現其中的方法;
        在實現的方法中編寫: req=方法參數;
       然後在該Action中任何需要用到Session的地方使用req.getSession()......

     方法三:
          在工程的 web.xml中 設置session的有效期
        <session-config>
               <session-timeout>30</session-timeout>
       </session-config>

session-timeout元素用來指定默認的會話超時時間間隔,以分鐘爲單位。該元素值必須爲整數。如果session-timeout元素的值爲零或負數,則表示會話將永遠不會超時。 session-timeout的取值範圍是1-1440。


setMaxInactiveInterval和session-config的比較:

1)、setMaxInactiveInterval的優先級高,如果setMaxInactiveInterval沒有設置,則默認是session-config中設置的時間。
2)、setMaxInactiveInterval設置的是當前會話的失效時間,不是整個web服務的。
3)、setMaxInactiveInterval的參數是秒,session-config當中配置的session-timeout是分鐘。

3. action中清除session的三種方法  【參考http://blog.sina.com.cn/s/blog_654d69690100vwsr.html】

第一種方法==》繼承SessionAware類來取得session,然後用invalidate()方法清理
public class ExitAction extends ActionSupport implements SessionAware{

      @Override
      public String execute() throws Exception {
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpSession session1 = request.getSession();
            session1.invalidate();//剛開始嘗試的是這種方法,但是沒有效果,終於發現原因是沒有繼承SessionAware
            return super.execute();
      }

      public void setSession(Map arg0) {

      }  

}


第二種方法 ==》用ActionContext取session,然後用clear()方法清理
public class ExitAction extends ActionSupport{

      @Override
      public String execute() throws Exception {
            ActionContext ac = ActionContext.getContext();
            Map session = ac.getSession();
            session.clear();
            return super.execute();
      }
}


第三種方法 ==》一樣用ActionContext取session,然後取一個Session的KEY,清除該KEY的session,這種辦法可以選擇性的清理你要清理的session
public class ExitAction extends ActionSupport{

       @Override
       public String execute() throws Exception {
             ActionContext ac = ActionContext.getContext();
             Map session = ac.getSession();
             session.remove("buser");
             session.remove("guser");
             session.remove("fuser");
            return super.execute();
     }
}

                      ------------------------------------------------------------------------------------------------------------------

我的方法是:

     public void logout() throws IOException{
            HttpServletRequest request = ServletActionContext.getRequest();//獲取action上下文中request
            String name=(String) request.getSession().getAttribute("userName");//通過request取得session中的userName屬性
            System.out.println(" before logout:"+name);
            request.getSession().removeAttribute("userName");//通過調用removeAttribute()方法刪除該屬性
            result="ok";
            name=(String) request.getSession().getAttribute("userName");
            System.out.println(" after logout:"+name);//輸出after logout null
            ServletActionContext.getResponse().getWriter().print(result);//把結果往前端傳回
        }

 實現退出登錄:
在頁面上單擊“退出登錄”按鈕----->觸發事件通過ajax訪問action【LogoutAction.java】----->採用上述方法清除用戶名------>返回到js文件中,用window.location.reload達到刷新本頁效果----->此時登錄已退出

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章