This blog describe how to create cookies in Java servlet. The Java project used as demo in this project has been created using Eclipse and deployed and run on JBoss application server. However, any other web container like Tomcat can be used to deploy and run this demo project.

As a first step, create a dynamic web project in Eclipse named CookieCreationDemo with the following structure

Create a dynamic web project for cookies

The servlet class CookieServlet will create cookie and send it back in the HTTP response. It is important to note here that the life of the cookie in this demo project is defined as one minute after which it will expire. We will verify the life-cycle of the cookie in a browser; preferably Chrome. Please make sure that Google Chrome is installed on your system to clearly understand the working of this demo.

Following is the code of the Java servlet

CookieServlet.java
package com.planetofbits.cookie.demo.servlet;

import java.io.IOException;

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

public class CookieServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		// Create a cookie object
		Cookie cookie = new Cookie("democookie", "This is cookie value");
		// Sets the maximum age in seconds for this Cookie
		cookie.setMaxAge(60); // This cookie will live for 60 seconds
		
		// Send the cookie back in response
		resp.addCookie(cookie);
	}
}

The web.xml looks like this

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>CookieCreationDemo</display-name>

	<servlet>
		<servlet-name>CookieServlet</servlet-name>
		<servlet-class>com.planetofbits.cookie.demo.servlet.CookieServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>CookieServlet</servlet-name>
		<url-pattern>/cookie</url-pattern>
	</servlet-mapping>
</web-app>

Deploy the web application and invoke it in the Google Chrome browser.

Note
Please use the port number according to your server installation.
Invoke the servlet

Invoking the web application in the browser makes a call to the servlet which creates the cookie and sends it back in the HTTP response. This can be confirmed by checking the localhost cookies in Chrome.

Check the cookie in Chrome

The interesting thing to note here is that after one minute (which is the life of the cookie which we have set in the servlet), the cookie expires and is removed from the browser. This can be confirmed by checking the localhost cookies one minute after invoking the web application.

How to create cookies in Java servlet
        

Comments, Questions or Suggestions: