Cookie基本操作(JSF)

public static void addCookie(String cookieName, String cookieValue){
FacesContext facesContext = FacesContext.getCurrentInstance();
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setMaxAge(31536000);
cookie.setPath("/");
((HttpServletResponse)facesContext.getExternalContext().getResponse()).addCookie(cookie);
}

public static String getCookieValue(String cookieName){
FacesContext facesContext = FacesContext.getCurrentInstance();
Cookie cookie[] = ((HttpServletRequest)facesContext.getExternalContext().getRequest()).getCookies();
if(cookie != null && cookie.length > 0){
String name = null;
for(int i = 0; i < cookie.length; i++){
name = cookie[i].getName();
if(name.equals(cookieName)){
          return cookie[i].getValue();
    }
}
}
return null;
}

public static void deleteCookie(String cookieName){
FacesContext facesContext = FacesContext.getCurrentInstance();
Cookie cookie[] = ((HttpServletRequest)facesContext.getExternalContext().getRequest()).getCookies();
if(cookie != null && cookie.length > 0){
String name = null;
for(int i = 0; i < cookie.length; i++){
name = cookie[i].getName();
if(name.equals(cookieName)){
cookie[i].setMaxAge(0);
cookie[i].setPath("/");
((HttpServletResponse)facesContext.getExternalContext().getResponse()).addCookie(cookie[i]);
    }
}
}
}

你可能感兴趣的:(JSF)