A cookie is a object with key value pair to store information related to the customer. Main objective is to personalize the customer's experience.
An utility method can be created like
private Cookie createCookie(String cookieName, String cookieValue) {
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setPath("/");
cookie.setMaxAge(MAX_AGE_SECONDS);
cookie.setHttpOnly(true);
cookie.setSecure(true);
return cookie;
}
If storing important information then we should alsways put setHttpOnly so that the cookie cannot be accessed/modified via javascript. setSecure is applicable if you are want cookies to be accessed only over https protocol.
using above utility method you can add cookies to response as
Cookie cookie = createCookie("name","value");
response.addCookie(cookie);