Cookies In Servlet

Cookies are text data sent by server to the client and it gets saved at the client local machine. This is a small bits of information that a web browser send to browser and that are browser returns unchanged after visiting the same page. Server can accept multiple cookies from client and we can disable cookies to get stored at client side from browser preferences. We can create, read, and send cookies to client browser. Class of the cookie can be written in the form of javax.servlet.http.Cookie. Cookie class has a single constructor. This that takes name and value parameters as they are mandatory.

Methods Of Cookies

  1. getComment(): This method is used by client side and returns the comment describing the cookie.
  2. getName(): This method is used by client as well as server side and used to return the name of the cookie.
  3. getValue(): This method is used to return the value of the cookie as String.
  4. getPath(): This method is used to return the path on the server to which browser returns this cookie.
  5. getDomain(): This method is used to return the domain name for cookie. Also we can use setDomaim() to set the domain name for cookie.
  6. getSecure(): This method returns the true if the browser is sending cookies only over a secure protocol and if it is false browser can use any protocol to send the cookie.
  7. getMaxAge(): This method returns the maximum age of the cookie. Age is specified in the second.
  8. getVersion(): This method returns the version of the protocol.
  9. isHttpOnly(): This method is used to check whether cookie has been marked as HttpOnly.

We can create Cookie as follows using:

    Cookie cookie = new Cookie(“myCookie”, “value”);

Sending cookies to the client:
Following are the steps involved in the sending cookies to the client

  1. Creating a cookie object: If we want to send the cookie to the client first step is creating the cookie using Cookies constructor with a cookie name and a cookie value.
  2. Setting the maximum age: second step is to setting the maximum age for the cookie. We can use setMaxAge() to specify how long the cookie should be valid.
  3. Placing the cookies into the HTTP response headers: This is the main step in sending request to the client. We can use response.addCookie() to send the cookie to client.

Cookies Example

Let us write a simple program to see how cookies work.
Listing 1:CookiesDemo



import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookiesDemo extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)

	throws IOException {


		PrintWriter printWriter = response.getWriter();

		Cookie cookie = new Cookie("url", "");

		cookie.setMaxAge(10 * 10);


		printWriter.println("Cookies created");

Listing 2:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	xmlns="" xmlns:web=""
	id="WebApp_ID" version="3.0">




Execute the CookiesDemo file in Eclipse and an output as below would be seen:


Advantages and Disadvantages Of Cookies In Servlet

Now let us see some pros and cons of using Cookies:

  • First advantage of the cookies is, it is very simple to use
  • Another advantage of the cookie is there is no need to send data back to the as browser can participate in this task.
  • One of the disadvantage of the cookies is that size and number of cookies are limited
  • The most common disadvantage of the cookies is, it not secure it can be modified by anyone.
  • It won’t work if the security level set too high in browser.


Previous Tutorial : How To Get Client IP Address In Servlet || Next Tutorial : 

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!