Servlet Cookie Example Tutorial

Cookies are used a lot in web client-server communication, it’s not something specific to java.

Some of the common usage of cookies are:

Session authentication using Cookies, we learned in Servlet Session Tutorial that HttpSession uses “JSESSIONID” cookie to keep track of the user session.

Personalized response to the client based on their preference, for example we can set background color as cookie in client browser and then use it to customize response background color, image etc.

Cookie

Cookies are text data sent by server to the client and it gets saved at the client local machine. When client send request to server, it passes the cookies stored by the server in request header like below:

Cookie Test="Test Cookie5"

Client can send multiple cookies to server and we can disable cookies to get stored at client side from browser preferences. Apart from the key-value pairs, server sends some other data to client in response header and it looks something like below.

Note that server sends some additional information for cookie, such as comment, domain, maximum time before cookie expires and Path where browser should send the cookie back in request. But when client sends cookie to browser, it only sends the name and value of the cookie.

HttpServletRequest getCookies() method is provided to get the array of Cookies from request, since there is no point of adding Cookie to request, there are no methods to set or add cookie to request.

Similarly HttpServletResponse addCookie(Cookie c) method is provided to attach cookie in response header, there are no getter methods for cookie.

Cookie class has a single constructor that takes name and value because they are mandatory parameters for a cookie, all other parameters are optional.

Some important methods of Cookie class are:

getComment() – Returns the comment describing the purpose of this cookie, used at client side. Note that server doesn’t receive this information when client sends cookie in request header. We can use setComment() method to set cookie description at server side.

getDomain() – returns the domain name for the cookie. We can use setDomain() method to set the domain name for cookie, if domain name is set then the cookie will be sent only to that particular domain requests.

getMaxAge() – returns the maximum age in seconds. We can use setMaxAge() to set the expiration time of cookie.

getName() – returns the name of the cookie, can be used at both browser and server side. There is no setter for name, we can set name once through constructor only.

getPath() – Returns the path on the server to which the browser returns this cookie. We will see it’s example where the cookie will be sent to specific resource only. We can use setPath() to instruct browser to send cookie to a particular resource only.

getSecure() – Returns true if the browser is sending cookies only over a secure protocol, or false if the browser can send cookies using any protocol. We can use setSecure() method to instruct browser to send cookie only over secured protocol.

getValue() – returns the value of the cookie as String. There is also setValue() method to change the value of cookie.

getVersion() – Returns the version of the protocol this cookie complies with. There is also a setter method for version.

isHttpOnly() – Checks whether this Cookie has been marked as HttpOnly. There is also a setter method that we can use to instruct client to use it for HTTP only.

Servlet Cookie Example

We will create two simple servlets to print cookies from client, in one of the servlet we will set a cookie for every domain and a cookie with Path settings so that other servlet won’t receive this from client.

The project structure will look like below image.

SetCookie.java: This servlet will set some cookies and send it to browser. It will also print cookie information and send it as HTML response.

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Google Plus, Facebook or Twitter. I would love to hear your thoughts and opinions on my articles directly.
Recently I started creating video tutorials too, so do check out my videos on Youtube.

8 Responses to "Servlet Cookie Example Tutorial"

I think there is a typo in referencing the servlet classes, – you call them as SetServlet and GetServlet in your comments but the real name of classes (servlets) are SetCookie and GetCookie respectively.