Cookies In Servlet

SHARE & COMMENT :

Cookies are text data sent by server to the client and it gets saved at the client local machine. This is a small bits of information that a web browser send to browser and that are browser returns unchanged after visiting the same page. Server can accept multiple cookies from client and we can disable cookies to get stored at client side from browser preferences. We can create, read, and send cookies to client browser. Class of the cookie can be written in the form of javax.servlet.http.Cookie. Cookie class has a single constructor. This that takes name and value parameters as they are mandatory.

Methods Of Cookies

  1. getComment(): This method is used by client side and returns the comment describing the cookie.
  2. getName(): This method is used by client as well as server side and used to return the name of the cookie.
  3. getValue(): This method is used to return the value of the cookie as String.
  4. getPath(): This method is used to return the path on the server to which browser returns this cookie.
  5. getDomain(): This method is used to return the domain name for cookie. Also we can use setDomaim() to set the domain name for cookie.
  6. getSecure(): This method returns the true if the browser is sending cookies only over a secure protocol and if it is false browser can use any protocol to send the cookie.
  7. getMaxAge(): This method returns the maximum age of the cookie. Age is specified in the second.
  8. getVersion(): This method returns the version of the protocol.
  9. isHttpOnly(): This method is used to check whether cookie has been marked as HttpOnly.

We can create Cookie as follows using:

    Cookie cookie = new Cookie(“myCookie”, “value”);

Sending cookies to the client:
Following are the steps involved in the sending cookies to the client

  1. Creating a cookie object: If we want to send the cookie to the client first step is creating the cookie using Cookies constructor with a cookie name and a cookie value.
  2. Setting the maximum age: second step is to setting the maximum age for the cookie. We can use setMaxAge() to specify how long the cookie should be valid.
  3. Placing the cookies into the HTTP response headers: This is the main step in sending request to the client. We can use response.addCookie() to send the cookie to client.

Cookies Example

Let us write a simple program to see how cookies work.
Listing 1:CookiesDemo

package javabeat.net.servlets;

import java.io.IOException;
import java.io.PrintWriter;

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

public class CookiesDemo extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)

	throws IOException {

		response.setContentType("text/html");

		PrintWriter printWriter = response.getWriter();

		Cookie cookie = new Cookie("url", "javabeat.com");

		cookie.setMaxAge(10 * 10);

		response.addCookie(cookie);

		printWriter.println("Cookies created");
	}
}

Listing 2: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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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">

	<servlet>
		<servlet-name>CookiesDemo</servlet-name>
		<servlet-class>javabeat.net.servlets.CookiesDemo</servlet-class>
	</servlet>

    <servlet-mapping>
		<servlet-name>CookiesDemo</servlet-name>
		<url-pattern>/CookiesDemo</url-pattern>
	</servlet-mapping>

</web-app>

Output
Execute the CookiesDemo file in Eclipse and an output as below would be seen:

servlet_cookies_demo

Advantages and Disadvantages Of Cookies In Servlet

Now let us see some pros and cons of using Cookies:

  • First advantage of the cookies is, it is very simple to use
  • Another advantage of the cookie is there is no need to send data back to the as browser can participate in this task.
  • One of the disadvantage of the cookies is that size and number of cookies are limited
  • The most common disadvantage of the cookies is, it not secure it can be modified by anyone.
  • It won’t work if the security level set too high in browser.

 

Previous Tutorial : How To Get Client IP Address In Servlet || Next Tutorial : 

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better