How to handle cookies in JSP?

In the previous article, we have seen How to write filters in JSP. In this article, We will discuss Cookies and the Handling of cookies in JSP, the best way to store and retrieve cookies in JSP.

Today, many websites use string text called cookies to store persistent client-side state between connection. Cookies are helpful for storing session ID, shopping cart contents, login credentials, etc.

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 now, 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.

[Page content]

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


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 JSP Cookies

  • public void setDomain(String pattern): This method sets the domain to which cookie applies. for eg
  • 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 JSP?

  • Create a cookie
Cookie cookie=new Cookie("key","value");
  • Set the maximum age
  • Send the cookie

How to handle Cookies in JSP?

Let’s see an example of Remember username and password in JSP using cookies.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
    Cookie[] cookies = request.getCookies();
    String username = "";
    String password = "";
    if (cookies != null) {
      for (int i = 0; i < cookies.length; i++) {
        Cookie cookie1 = cookies[i];
        if (cookie1.getName().equals("cookie-username")) {
          username = cookie1.getValue();
        } else if (cookie1.getName().equals("cookie-password")) {
          password = cookie1.getValue();
  <form action="welcome.jsp">
    <table bgcolor="#A5D8F3">
        <td><input type="text" name="uname" value="<%=username%>"></td>
        <td><input type="password" name="pass" value="<%=password%>"></td>
        <td>Remember me</td>
        <td><input type="checkbox" name="remember" value="true"></td>
      <tr align="center">
        <td><input type="submit" value="Submit"></td>



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
String name=request.getParameter("uname");
String pass=request.getParameter("pass");
String remember=request.getParameter("remember");
out.print("Username "+name+"<br>Password  "+pass);
  Cookie user= new Cookie("cookie-username",name);
  Cookie passw=new Cookie("cookie-password",pass);


Once again access the index.jsp page this time the username and password field will be auto-filled.

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.

Thus we have seen How to Handle cookies on the JSP page with a simple example.

In the next article of the tutorial, we will discuss How to handle sessions on the JSP page with a simple example.