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;
}
}