How to create cookie that does not destroyed when you close your browser

时间:2018-06-05 05:03:33

标签: jsf servlets cookies

Is it possible? if yest how to create the cookie that will be remember after browser closed, and reopen? I would like to remember user login in the logon field. I created it in that way:

FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest reuest = (HttpServletRequest) fc.getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
Cookie cGivenLogin = new Cookie("cGivenLogin", givenLogin);
cGivenLogin.setMaxAge(892800);
response.addCookie(cGivenLogin);

and how to read cookie:

FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
Cookie[] cookies = request.getCookies();

if(cookies != null){
   for(int i=0; i < cookies.length; i++) {            
       String name = cookies[i].getName();
       String value = cookies[i].getValue();
       if(name.equals("cGivenLogin")){
         givenLogin = value;}
      }
}

it works but only until close the browser. Session timeout is set up to 8h, is it possible to remember cookies after close browser?

0 个答案:

没有答案