.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Cookies in servlet

Posted By:satyapriyanayak       Posted Date: May 11, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will know how to use cookies in servlet.

Cookies: - This is a memory location present in users machine to store information about the user. A page available in a server can create variables in the users memory allocated for cookie. Whenever the user sends further request then the cookie variables created by the same server travel along the request. The destination page of the request can find value from the cookie variable present in the request. These variables can be available in users machine for a period of 24 hours by default. The life span of cookie variables can be changed by the server. The user can deny creation of cookie variables.

Creation process of cookie variable

1.The servlet class must be created by inheriting javax.servlet.http.httpservlet
2.Create an object of javax.servlet.http.cookie.By using name of the cookie variable and its value in the constructor.
3.Add the object of cookie class into the response by using addCookie () method of Httpservlet response.The default lifespan of cookie variable can be changed by using the required number of second into setMaxAge() method of the cookie class.

Reading process of cookie variable

1.The servlet class must be created by inheriting javax.servlet.http.httpservlet.
2.Find all cookie present in the request by using getCookies() method of HttpservletRequest .This method returns an array of cookie class.
3.Find name of cookie variable by using getName() or find value of cookie variable by using getValue() method.

Example: -

Create an html file as below in context folder
To visible directory listing Go to E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf
In web.xml file change (false to true) below

<body bgcolor="yellow">
<form action="./create" method = "get" >
<h2>Enter Product Name<input type="text" name="t1" ></h2>
<input type="submit" value="submit">

create.java file
//Creating cookie
import  javax.servlet.*;
import  javax.servlet.http.*;
import java.io.*;

public class create extends HttpServlet
    public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
    String s1=req.getParameter("t1");


//Cookie creation process
Cookie c=new Cookie("pname",s1);
  PrintWriter out=res.getWriter();

  out.println("<h1 align='center'>Ur info</h1>");
out.println("<h1>Price of : "+s1+" is 50000</h1>");
out.println("<h2><a href='./buy'>Buy now</a> </h2>");

buy.java file

//Reading cookie/session variable
import  javax.servlet.*;
import  javax.servlet.http.*;
import java.io.*;

public class buy extends HttpServlet
    public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException

  //Reading value of session variable
  HttpSession ses=req.getSession();
  String s1=(String)ses.getAttribute("pn");

//Reading value of cookie variable
   Cookie c[]=req.getCookies();
  for(int i=0;i<c.length;i++)
  PrintWriter out=res.getWriter();
out.println("<h1>Ur product: <font color='red'>"+s1+"</font> will be delivered soon</h1>");

web.xml settings

<?xml version="1.0" encoding="ISO-8859-1"?>
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at


  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  See the License for the specific language governing permissions and
  limitations under the License.
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"




Compile both the files as below

javac -cp servlet-api.jar create.java (for tomcat 6.0)
javac -cp servlet-api.jar buy.java (for tomcat 6.0)


Run the tomcat then write the below line in the Url


Here test is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

After giving the url a set a listing will come, here only one appears
As cookie.html,click it

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend