3:13 pm - Friday December 17, 4630

Cookies

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Cookies: 

Cookies are information that a Web server sends to a browser and that the browser returns back to web server when visiting the same Web site later.

Cookie can be saved in a browser as a key value pair

To add a cookie in browser we need to create an object for cookie class and use a HttpResponse object to send the data to browser.

To create cookie object:

            Cookie ck=new Cookie(“key”,”value”);        

To add cookie to browser:

        Response.addCookie(ck); // addCookie method takes cookie objects as argument

To get or retrieve the all the cookie present in the browser we need to use a HttpRequest object

To get cookie:


Cookie ck[]=request.getCookies();

To display cookie present in cookie array:


for(int i=0;i<ck.length;i++){

String str=ck[i].getName();

String value=ck[i].getValue();

out.println("<b><br>Name of the Cookie :"+str);

out.println("<b><br> Value of the cookie :"+value);

}

Project Structure:

 

 

 

 

 

 

 

 

 

 

 AddCookie.java:

<pre>package com.candidjava;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

/**
 * Servlet implementation class AddCookie
 */
public class AddCookie extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public AddCookie() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		String str = req.getParameter("Name");
		System.out.println(str);
		// Cookie(key,value);
		Cookie ck = new Cookie("key", str);
		PrintWriter out = res.getWriter();
		res.setContentType("text/html");
		// HttpServletResponse.addCookie(Cookie);
		res.addCookie(ck);
		out.println("<html>");
		out.println("<body bgcolor=pink>");
		out.println("<font color=blue size=8>");
		out.println("Your cookie is set to " + str);

		out.println("<p> To get a cookie value press the button");
		out.println("<form action=GetCookie>");
		out.println("<input type=submit value=press>");
		out.println("</form>");

		out.println("</font>");
		out.println("</body>");
		out.println("</html>");
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}

GetCookie.java:

<pre>package com.candidjava;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GetCookie
 */
public class GetCookie extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetCookie() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		res.setContentType("text/html");
		PrintWriter out = res.getWriter();
		Cookie ck[] = req.getCookies();
		out.println("<html>");
		out.println("<body bgcolor=pink>");
		out.println("<font color=blue size=8>");
		out.println("<br>Number of cookie=" + ck.length);
		for (int i = 0; i < ck.length; i++) {
			String str = ck[i].getName();
			String value = ck[i].getValue();
			out.println("<b><br>Name of the Cookie :" + str);
			out.println("<b><br> Value of the cookie :" + value);
		}
		out.println("</body></html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

}

web.xml:

<pre><?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Cookies</display-name>
	<servlet>
		<description>
		</description>
		<display-name>AddCookie</display-name>
		<servlet-name>AddCookie</servlet-name>
		<servlet-class>
		com.candidjava.AddCookie</servlet-class>
	</servlet>
	<servlet>
		<description>
		</description>
		<display-name>GetCookie</display-name>
		<servlet-name>GetCookie</servlet-name>
		<servlet-class>
		com.candidjava.GetCookie</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>AddCookie</servlet-name>
		<url-pattern>/AddCookie/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>GetCookie</servlet-name>
		<url-pattern>/GetCookie</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

OUTPUT:


 DOWNLOAD+SOURCE CODE                                                                                                                                                      BACK

Filed in: Z-A Servlet codes

No comments yet.

Leave a Reply