Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

cookie jsf

public class CookieHelper {

  public void setCookie(String name, String value, int expiry) {

    FacesContext facesContext = FacesContext.getCurrentInstance();

    HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
    Cookie cookie = null;

    Cookie[] userCookies = request.getCookies();
    if (userCookies != null && userCookies.length > 0 ) {
        for (int i = 0; i < userCookies.length; i++) {
            if (userCookies[i].getName().equals(name)) {
                cookie = userCookies[i];
                break;
            }
        }
    }

    if (cookie != null) {
        cookie.setValue(value);
    } else {
        cookie = new Cookie(name, value);
        cookie.setPath(request.getContextPath());
    }

    cookie.setMaxAge(expiry);

    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
    response.addCookie(cookie);
  }

  public Cookie getCookie(String name) {

    FacesContext facesContext = FacesContext.getCurrentInstance();

    HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
    Cookie cookie = null;

    Cookie[] userCookies = request.getCookies();
    if (userCookies != null && userCookies.length > 0 ) {
        for (int i = 0; i < userCookies.length; i++) {
            if (userCookies[i].getName().equals(name)) {
                cookie = userCookies[i];
                return cookie;
            }
        }
    }
    return null;
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to delete an element from an array in java data structure 
Java :: org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:876) 
Java :: no of words in a string in java 
Java :: keep jframe on top 
Java :: linked list vs vector 
Java :: default java keystore 
Java :: afficher matrice java 
Java :: default case in Java switch-case 
Java :: springbootservletinitializer maven dependency 
Java :: Java boolean Keyword 
Java :: regex pattern for valid identifiers 
Java :: how to run few methods of class after mockStatic 
Java :: enhance for loop java 
Java :: how to generate random large string in java 
Java :: why are my if statements not working with inputs in java 
Java :: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251) 
Java :: android studio clear views of layout 
Java :: Java Hashmap Replace Elements 
Java :: next method jdbc 
Java :: Java Create a ByteArrayInputStream 
Java :: install oracle java ubuntu 20.04 
Java :: isWhiteSpace java 
Java :: how to change background color of grid in grid layout in android 
Java :: arrondi java 
Java :: get field name java 
Java :: fragment call activity 
Java :: java delete element from list 
Java :: change color in recyclerview 
Java :: Java Create a ByteArrayOutputStream 
Java :: set up a tree in java 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =