- Java EE Tutorials
- Servlets Tutorials
- Servlets Interview Questions
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
- getComment(): This method is used by client side and returns the comment describing the cookie.
- getName(): This method is used by client as well as server side and used to return the name of the cookie.
- getValue(): This method is used to return the value of the cookie as String.
- getPath(): This method is used to return the path on the server to which browser returns this cookie.
- getDomain(): This method is used to return the domain name for cookie. Also we can use setDomaim() to set the domain name for cookie.
- 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.
- getMaxAge(): This method returns the maximum age of the cookie. Age is specified in the second.
- getVersion(): This method returns the version of the protocol.
- 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
- 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.
- 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.
- 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:
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 :