Setting cookies with servlet involves three steps:
1) Creating a Cookie object: You call the Cookie constructor with a cookie name and a cookie value, both of which are strings.
Cookie cookie = new Cookie("key","value");
2) Setting the maximum age: You use setMaxAge to specify how long (in seconds) the cookie should be valid. Following would set up a cookie for 24 hours.
3) Sending the Cookie into the HTTP response headers: You use response.addCookie to add cookies in the HTTP response header as follows: