Explain Cookie in Servlet with example?


            Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.

            A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.

 

Creating a cookie and store in browser

Cookie ck=new Cookie("key","value");

 

To add cookie expiry time

ck.serMaxAge(600); // seconds ie 10 min

 

To add Cookie to HTTP response header.

response.addCookie(ck);

  

The browser is expected to support 20 cookies for each Web server, 300 cookies total, and may limit cookie size to 4 KB each.

 

To retrieve Cookie from browser

Cookie cks[]=request.getCookies();

 

To remove a Cookie, just set the value as null and age as 0.

Cookie ck=new Cookie("key",null);
       ck.setMaxAge(0);
       response.addCookie(ck);

 

Example

To create a cookie

package com.candidjava;

import java.io.IOException;

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

/**
 * Servlet implementation class CookieController
 */
public class CookieController extends HttpServlet {
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String s1=request.getParameter("cookie1");
		String s2=request.getParameter("cookie2");
		String s3=request.getParameter("cookie3");
		
		Cookie ck1=new Cookie("ck1", s1);
		Cookie ck2=new Cookie("ck2", s2);
		Cookie ck3=new Cookie("ck3", s3);
		
		response.addCookie(ck1);
		response.addCookie(ck2);
		response.addCookie(ck3);
		
		response.sendRedirect("success.html");
		
	}

}

To retrieve a cookie

 package com.candidjava;

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

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

/**
 * Servlet implementation class CookieGetController
 */
public class CookieGetController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		
		out.print("<h1> Retreiving all cookies from browser localhost domain </h1> <br>");
		
		Cookie[] cks= request.getCookies();
		for (Cookie cookie : cks) {
			
			out.print("<br>Cookie Name :" + cookie.getName());
			out.print("<br>Cookie Value :" + cookie.getValue());
			out.print("<br>");
			
		}
		
	}
}

Screenshot

            

             

            


Download

Cookie example war

Cookie example zip



Comments


©candidjava.com