# Delete Cookies in Java (on Server Side)

In 
Published 2022-12-03

This tutorial explains to you how the cookies might be deleted from the server side in Java.

HTTP Cookies are little pieces of data which is send from the browser to the application server and from the application server to the browser. The server use this information (cookie) for identifying the request/ the user (for session management). This data exchange happens when a cookie is created. If the cookie is not created, no cookie is sent by the browser.

The cookies could be created by JavaScript directly on the browser or by a servlet on the server. When the cookie is created by the server it will send a response to the browser containing the cookie (in the header response). In this case the browser will create a cookie locally and will send that cookie to that web site if the cookie is not expired (when the cookie is expired it will be deleted from the browser and will not be used until another cookie will be created instead).

For showing this behavior, I will create a Dynamic Web Application under Eclipse and I will use WildFly as Java Application Server and Google Chrome as browser.

First, here are the elements of the application :

  • 2 HTML page (the index.html and page2.html)
index.html
<html>
  <head>
    <meta charset="ISO-8859-1">
  </head>
  <body>
    <p> This is the <b> index.html </b> page. </p>
        
    <form action="MyJavaServlet" method="POST">
         Name: <input type="text" name="name">
         <br>
         Telephone#: <input type="text" name="telephone">
         <input type="submit" value="Submit">
    </form>
    
   <p style="margin-top: 10; margin-bottom: 0">Link to another HTML page: 
     <a href="http://localhost:8080/Java_cookies/page2.html">Page2.html</a></p>
    
   <p style="margin-top: 10; margin-bottom: 0">
   Link to the Java servlet for removing the cookie: 
     <a href="http://localhost:8080/Java_cookies/MyJavaServletDeleteCookies"> 
   MyJavaServletDeleteCookies </a></p>
 </body>  
</html>
page2.html
<html>
  <head>
      <meta charset="ISO-8859-1">
  </head>
  <body>
      <p> This is the <b> page2.html </b> page. </p>
 
      <form action="MyJavaServlet" method="POST">
         Name: <input type="text" name="name">
         <br>
         Telephone#: <input type="text" name="telephone">
         <input type="submit" value="Submit">
      </form>
       
      <p style="margin-top: 10; margin-bottom: 0">Link to another HTML page: 
        <a href="http://localhost:8080/Java_cookies/index.html"> index.html</a></p>
  </body>  
</html>
  • 2 Java servlet which create & remove the cookie
package servlets;
 
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class MyJavaServlet
 */
@WebServlet("/MyJavaServlet")
public class MyJavaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyJavaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // TODO Auto-generated method stub
 
        PrintWriter myResponse = response.getWriter();
         
        String message = "This is your response from a POST call. <br> The response is a Java Servlet !";
         
        Cookie name = new Cookie("name",  request.getParameter("name"));
        
        // Set expiry date after 1 hour for this cookie.
        name.setMaxAge(60*60*2);
        name.setDomain("localhost");
        name.setPath("/");
         
        // Add the cookie to the response header.
        response.addCookie(name);
        
        myResponse.println( "\n" + 
             "<title> Example of using Java Servlet with POST Call Method !</title>\n" +
             "\n" +
             "<h2 align="\"center\"">" + message + "</h2>\n" +
             "<ul>\n" +
             "  <li><b>You entered the name </b>: "       + request.getParameter("name") + "\n" +
             "  </li><li><b>You entered the telephone# </b>: " + request.getParameter("telephone") + "\n" +
             "</li></ul>\n <p>   </p>  <p>Link to HTML page : "+
             "  <a href="http://localhost:8080/Java_cookies/page2.html">link</a></p> " +
             "");
   
    }
}
package servlets;
 
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class MyJavaServlet
 */
@WebServlet("/MyJavaServletDeleteCookies")
public class MyJavaServletDeleteCookies extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyJavaServletDeleteCookies() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // TODO Auto-generated method stub
 
        PrintWriter myResponse = response.getWriter();
         
        String message = "This Java Servlet will Remove a cookie !";
         
        Cookie name = new Cookie("name",  request.getParameter("name"));
        
        // The cookie will expire and the browser will remove the cookie.
        name.setMaxAge(0);
        name.setDomain("localhost");
        name.setPath("/");
         
        // Add the cookie to the response header.
        response.addCookie(name);
        
        myResponse.println( "\n" + 
             "<title> This Java Servlet will Remove a cookie ! </title>\n" +
             "\n" +
             "<h2 align="\"center\"">" + message + "</h2>\n" +
             "<ul>\n" +
             "  <li><b>You entered the name </b>: "       + request.getParameter("name") + "\n" +
             "  </li><li><b>You entered the telephone# </b>: " + request.getParameter("telephone") + "\n" +
             "</li></ul>\n <p>   </p>  <p>Link to HTML page : "+
             "  <a href="http://localhost:8080/Java_cookies/page2.html">link</a></p> " +
             "");
    
    }
}

In order to check how the cookies are created/ removed you have to check this into the browser.