Cookies Class in Servlet Explained with Example | Using Cookies for Session Management in Servlet

In this tutorial, we will learn the session management technique. There are four techniques for session management in servlet. Here we will discuss the first techniques to manage the session. Among all of them, Cookie class is the first and foremost technique to manage the session. Before discussing the Cookie Class in Servlet let’s first understand the cookie.

What are Cookies?

Cookies are the small files that are stored in the cache memory of your computer browser so that your browser and websites can track your browsing session and save useful information, likewise account name and password for later retrieval. Cookies data are stored in the client browser. Because a cookie value can uniquely identify a client, cookies are often used for session tracking.

The Cookie Class

The servlet API provides the javax.servlet.http.Cookie class to work with cookies. You create a cookie with the cookie() constructor. A servlet can write the cookie to a user’s machine via the addCookie() method of the HttpServlet Response interface.

Cookies Class in Servlet Explained with Example 1

The data for those cookies are included in the HTTP response Header that is sent to the browser. The name and the values of the cookie are stored in the client browser. The information saved for each cookie contains the following.

  1. The name of the cookie
  2. The value of the cookie
  3. The expiration date of the cookie
  4. The domain and path of the cookie

Do Refer:

Types of Cookies

There are two different types of cookies namely. They are as follows

  • Session Cookies
  • Persistent Cookies

Session Cookies: These cookies don’t have any expiration time and exist in the browser memory. On Closing the Browser, Cookies will be destroyed automatically.

Persistent Cookies: These cookies have an expiration time and are stored in the hard drive of the user. Persistent Cookies will be destroyed depending on the expiration time.

How to Setup Cookies with Servlet?

Setting Cookies in Servlet includes three different steps and they are as follows

  • Firstly you need to create a cookie object.
  • The next step is to set the maximum age.
  • After that place the Cookie in HTTP Response Header.

Creating a Cookie Object: You can call the Cookie Constructor using the Cookie Name and Cookie Value and both of them are strings.

 
Cookie cookie = new Cookie("key","value");

Set the Maximum Age: You can specify how long the cookie should be valid by setting the maximum age. The below code will set up a maximum age of 24 hours.

cookie.setMaxAge(60 * 60 * 24);

Place the Cookie in the HTTP Response Header: You can use the response.addCookie in order to add cookies in the HTTP Response Headers.

response.addCookie(cookie);

Reading Cookies with Servlet

In order to read cookies, you need to create an array of javax.servlet.http.Cookie Objects by calling the getCookies() method of HttpServletRequest. After that cycle through the array and utilize the getName() and getValue() methods to access cookies and their associated values.

Methods of the Cookie Class

1. public void addCookie(cookie ck): A servlet can write the cookie to a user’s machine via the addCookie() method of the HttpServletResponse interface.
2. public Cookie[] getCookie(): A servlet retrieves cookies by calling the getCookies() method of HttpServlet Response.
3. public void setMaxAge(int sec): Sets the maximum age of the cookie to secs. After this number of seconds the cookie is deleted.
4. public void setPath(String p): It specifies a path for the cookies
5. public void setComment(String c): It sets the comment field of the cookie.
6. public void setVersion(int v): It sets the cookie protocol version to v which will be 0 or 1.
7. public String getName(): It returns the name of the cookie.
8. public String getPath(): It returns the path of the cookie.
9. public int getMaxAge(): It returns the age(in seconds).|
10. public int getVersion(): It returns the cookie protocol version(will be 0 or 1)

Advantages of Cookies

  • It is the simplest technique for maintaining the state.
  • Cookies are usually maintained at the client-side.

Disadvantages of Cookies

  • One drawback of the Cookies is that they don’t work if they are disabled on the browser.
  • We can only send cookie information into the object.

Cookies in Servlet Example

In this example, we are storing the username and password in the cookie object and accessing it in another servlet.

Index.html file:

Cookies Class in Servlet Explained with Example 2

FirstServlet.java file:

Cookies Class in Servlet Explained with Example 3

SecondServlet.java file:

Cookies Class in Servlet Explained with Example 4

web.xml file:

Cookies Class in Servlet Explained with Example 5

Cookies Class in Servlet Explained with Example 6

Cookies Class in Servlet Explained with Example 7

Cookies Class in Servlet Explained with Example 8