Search code examples
jakarta-eecookiesweb-applicationsapplication-server

Is the number of cookies obtained from application server is 1?


Generally, container sends the cookies as part of the response. I have created a simple login form in JSF and trying to submit it to a JSF bean.

In my bean's action method, I am trying to retrieve the cookies obtained from the server, using the following code.

FacesContext ctx = FacesContext.getCurrentInstance();
Map cookieMap = ctx.getExternalContext().getRequestCookieMap();

To my knowledge, I know that we get one cookie i.e., JSESSIONID. I have checked with some servers using the below code.

if (cookieMap != null && !cookieMap.isEmpty()) {
            Iterator iterator = cookieMap.keySet().iterator();
            while (iterator.hasNext()) {
                Cookie cookie = (Cookie) cookieMap
                        .get((String) iterator.next());
                System.out.printf("Cookie get name %s, cookie get value %s ",
                        cookie.getName(), cookie.getValue());
            }
        }

Below are the results captured from different servers.

  1. VMware vFabric tc Server Developer Edition
output : Cookie get name JSESSIONID, cookie get value 3B88AE51A991800566A4C3A05AC36C37
  1. Tomcat v7.0 Server
Cookie get name JSESSIONID, cookie get value 115B310C64B68C3E25C49391236AE978
  1. Jetty server
Cookie get name JSESSIONID, cookie get value 115B310C64B68C3E25C49391236AE978

Based on the above statistics, I have coded in the following way to pass cookie to the url connection method as shown below.

private void openConnection(URL url, Cookie jsessionid){
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.disconnect();
    conn.setUseCaches(false);
    conn.setDefaultUseCaches(false);
    conn.setDoOutput(true);
    if (jsessionid != null) {
        conn.addRequestProperty("Cookie", jsessionid.getName() + "="
                + jsessionid.getValue());
    }
    conn.connect();
    //some other code
}

A senior developer improved the above code snippet by iteratively adding cookies from the cookieMap to the RequestProperty as shown below.

if(cookieMap !=null && !cookieMap.isEmpty()){
    Iterator iterator = cookieMap.keySet().iterator();
    while (iterator.hasNext()) {
          Cookie cookie = (Cookie) cookieMap.get((String)iterator.next());
          conn.addRequestProperty("Cookie",
                      cookie.getName() + "=" + cookie.getValue());
    }
}

I didn't try this with other application servers but I guess, other application servers like Weblogic, GlassFish, would also give the same output, i.e.., 1 cookie with name JSESSIONID. Correct me if I am wrong. I would like to know if the number of cookies obtained from the a container is 1 or more.


Solution

  • Senior developer correctly improved your code, because it is possible that application server sends more than one cookie. It merely depends on the Web application that serves your request; for example you can receive authentication cookie (represented by JSESSIONID) as well as persistent cookie at the same time. By this improvement, your application will sent back all received cookies.

    As an example, Web application would maybe like to remember the choice of the user language you selected during your first visit to the Web site. This information should be brought back by a special cookie that does not depend whether user has been authenticated when it comes later to the site.

    I didn't try this with other application servers but I guess, other application servers like Weblogic, GlassFish, would also give the same output, i.e.., 1 cookie with name JSESSIONID.

    That's correct. However, as pointed out above, you could receive other types of cookies together with JSESSIONID.