Cookies Handling in Servlet

As of now, we have discussed Exception in Servlet and how to Handle cookies in Servlet. In this article, we will discuss Cookies and the Handling of cookies in Servlet.

What are Cookies?

Cookies are data packets that the computer receives, then sends back without altering it. Whenever you visit any website, the website sends the cookie to your computer, your computer stores in the file located inside your web browser.

It is helpful in keeping track of your website visitors. As we see today, we have different passwords and we cannot handle so many passwords so, whenever we log in, the browser asks us to save the password and then we click on save.

The Set-Cookie HTTP response header sends a cookie from the server to the client. A cookie looks like this

Set-Cookie <cookie-name>=<cookie-value>

This shows the server sending header to tell clients to store a pair of cookies.

HTTP/2.0 404 NOT FOUND
Content-Type:text/html
Set-Cookie:cookie_1=abc
Set-Cookie:cookie_2=abc1234

[Page content]

Whenever client request to the server, the browser send back all previously-stored cookie to the server in the following way

GET/hello.page HTTP/2.0
Host:www.abc.org
Cookie:cookie_1=abc,cookie_2=abc1234

The Lifetime of cookie

  • Session Cookies: This is deleted when the current session ends.
  • Permanent Cookies: This is deleted as per the specified Expired  date attribute
Set-Cookie: id=1234; Expires=Fri, 10 Feb 2022 :15:00 GMT;

Methods of Servlet Cookies

  • public void setDomain(String pattern): This method sets the domain to which cookie applies. for eg https://codedec.com/
  • public String getDomain(): This method returns the domain to which the cookie applies.
  • public void setMaxAge(int expiry): This method sets the expiry of the cookie.
  • public int getMaxAge(): This method returns the age of the cookie specified in second.
  • public String getName(): This method returns the name of the cookie.
  • public void setValue(String value): This method sets the values associate with cookies.
  • public String getValue(): This method returns the value associated with the cookie.
  • public void setPath(String uri): This method sets the path to which cookies applied.
  • public String getPath(): This method returns the path to which cookies apply.
  • public void setSecure(boolean flag): This method sets the value indicating whether the cookie should only be sent over an encrypted network.
  • public void setComment(String purpose): This method sets the comment for the cookie.
  • public void getComment(): This method returns the comment of the cookie.

How to set Cookies in Servlet?

  • Create a cookie:
Cookie cookie=new Cookie("key","value");
  • Set the maximum age:
cookie.setMaxAge(60*60*24)
  • Send the cookie:
response.addCookie(cookie);

How to handle Cookies in Servlet

Example to Set cookies in servlet and Get the cookies in servlet

index.html

Servlet1.java

Servlet2.java

web.xml

Output

Uses of Cookie

  • Cookies are able to keep track of the website.
  • It is on the client-side.
  • Use to store login information of the user.