Java CookieUtils - Read cookie

By berend on Dec 14, 2012

Java code to read cookies, part of my WebAdmin project at BMT ARGOSS.

The cookies are set in the frontend of this web tool using javascript, the cookie includes which Database an user works on. (0/1/2)
This is the piece of Java code to read it.

package com.bmtargoss.smart.webadmin;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class CookieUtils {

    public static int getServerId(HttpServletRequest hreq) {
        int result = -1;

        /*
         * Get cookie which includes the database id. 
         */
        Cookie[] cookies = hreq.getCookies();
        if (cookies != null) {
          for (Cookie ck : cookies) {
            if ("database".equals(ck.getName())) {
                result = Integer.parseInt(ck.getValue());
            }
          }
        }

        return result;
    }

}

Comments

Sign in to comment.
berend   -  Dec 14, 2012

yep, I'll post some more soon as long the company agrees

 Respond  
Hawkee   -  Dec 14, 2012

Always great to see more Java code on the site.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.