public static void addCookie(
		HttpServletRequest request,
		HttpServletResponse response,
		String cookieName,
		String value,
		String comment,
		int maxAge,
		boolean secure,
		boolean contextOnlyPath
	) {
		Cookie newCookie = new Cookie(cookieName, value);
		if(comment!=null) newCookie.setComment(comment);
		newCookie.setMaxAge(maxAge);
		newCookie.setSecure(secure && request.isSecure());
		String path;
		if(contextOnlyPath) {
			path = request.getContextPath() + "/";
			//if(path.length()==0) path = "/";
		} else {
			path = "/";
		}
		newCookie.setPath(path);
		response.addCookie(newCookie);
	}
public static void addCookie ( HttpServletRequest request , HttpServletResponse response , String cookieName , String value , String comment , int maxAge , boolean secure , boolean contextOnlyPath ) { Cookie newCookie = new Cookie ( cookieName , value ) ; if ( comment != null ) newCookie . setComment ( comment ) ; newCookie . setMaxAge ( maxAge ) ; newCookie . setSecure ( secure && request . isSecure ( ) ) ; String path ; if ( contextOnlyPath ) { path = request . getContextPath ( ) + "/" ; } else { path = "/" ; } newCookie . setPath ( path ) ; response . addCookie ( newCookie ) ; }
Adds a cookie.