A cookie is an object defined with a key and a value as string. The cookie works in the web response. It is persistent because it can be registered as a file in the PC of the user. To create a cookie, simply instantiate it and register it in a response. Here's a sample:
Cookie cookie = new Cookie("myCookieKey", "myCookieValue");
cookie.setMaxAge(60);
response.addCookie(cookie);
We have created a cookie called myCookieKey with the value myCookieValue and a maximum duration of 60 seconds. In the preceding code, we create a second cookie, myHttpOnlyCookieKey, setting a more secure level. Thanks to the setHttpOnly(true) method, the cookie will never be registered on the client side.