Java examples for Servlet JSP:Servlet
Set new cookie by name, value and max age, with default path "/" from Servlet.
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Main{ /**/* ww w . ja v a 2 s . co m*/ * Set new cookie by name, value and max age, with default path "/". * * @param name Cookie name. * @param value Cookie value. * @param maxAgeInSeconds Cookie age in seconds. */ public static void setCookie(String name, String value, int maxAgeInSeconds) { setCookie(name, value, maxAgeInSeconds, "/"); } /** * Set new cookie by name, value, max age and path. * * @param name Cookie name. * @param value Cookie value. * @param maxAgeInSeconds Cookie age in seconds. * @param path Cookie path. */ public static void setCookie(String name, String value, int maxAgeInSeconds, String path) { HttpServletResponse response = ActionContext.getActionContext() .getHttpServletResponse(); Cookie cookie = new Cookie(name, value); cookie.setMaxAge(maxAgeInSeconds); cookie.setPath(path); response.addCookie(cookie); } }