cookie

 

private static final String VISITED_HOME_PAGE_COOKIE_NAME = "visitedIndex";

public String testAction(){

//比如在訪問這個action時要記錄日誌

HttpServletResponse response = ServletActionContext.getResponse();

Cookie cookie = WebUtils.getCookie(request, VISITED_HOME_PAGE_COOKIE_NAME );
        if( null == cookie ){
            cookie = new Cookie(VISITED_HOME_PAGE_COOKIE_NAME , “自定義內容”);

           //記錄日誌操作。。。
        }
        int expirestime = 2*12*30*24*60*60;  //設置cookie過期時間爲兩年,意思是就算關閉瀏覽器再次打開cookie也會存在;如果設置爲-1,意思是關閉瀏覽器下次訪問這個action時
        cookie.setMaxAge(expirestime); 
        cookie.setDomain("xxx.com");
        response.addCookie(cookie);

}

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