ServletUtilities.getCookieValue
public static String getCookieValue(Cookie[] cookies,
String cookieName,
String defaultVal) {
if (cookies != null) {
for(int i=0; i
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
}
return(defaultVal);
}
The getCookie method is similar
Returns the Cookie object instead of
the value
77
Servlets
www.corewebprogramming.com
Simple Cookie Setting Servlet
public class SetCookies extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
for(int i=0; i<3; i++) {
Cookie cookie = new Cookie("Session Cookie " + i,
"Cookie Value S" + i);
response.addCookie(cookie);
cookie = new Cookie("Persistent Cookie " + i,
"Cookie Value P" + i);
cookie.setMaxAge(3600);
response.addCookie(cookie);
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(...);
78
Servlets
www.corewebprogramming.com
39