Describing Cookies: A Cookie is small
information sent by a web server to a web client. Cookies are saved at the client
side for the given domain and path. The cookie file persists on the client
machine and the client browser returns the cookies to the original. Cookies are
small bits of textual information that a Web server sends to a browser and that
the browser returns unchanged when visiting the same Web site or domain later.
By having the server read information it sent the client previously, the site
can provide visitors with a number of conveniences. Whenever the browser
requests a resource, the cookie matching the domain and the path of the request
URL is sent to the WEB Server. Cookies are transmitted to the server through
HTTP headers in the request sent to the server.
The Servlet API provides a class named Cookie under the javax.servlet.http package.
It provides us a convenient way to create and read cookie data. to send data a
servlet use addCookie() method of the httpServletResponse. to retrieve the
cookie in a request returned by the browser, the Servlet uses the getCookies()
method.
Creating Cookies: A Cookie is created by calling the Cookie constructor, which takes two strings:
the cookie name and the cookie value. Neither the name nor the value should
contain whitespace or any of:
{[ ] ( ) = , " / ? @ : ;}
Cookie Attributes:
getComment/setComment: Gets or sets a comment associated with this
cookie.
getDomain/setDomain: Used to gets or sets the domain with which the
cookie is associated.
getMaxAge/setMaxAge: Gets or sets how much time (in seconds) should
elapse before the cookie expires. If you don't set this, the cookie will last
only for the current session (i.e. until the user quits the browser), and will
not be stored on disk.
getName/setName: Gets or sets the name of the cookie. The name and the
value are the two pieces you virtually always care about. .
getPath/setPath: Gets or sets the path to which this cookie applies. If
you don't specify a path, the cookie is returned for all URLs in the same
directory as the current page as well as all subdirectories.
getSecure/setSecure: Gets or sets the boolean value indicating whether
the cookie should only be sent over encrypted (i.e. SSL) connections.
getValue/setValue: Gets or sets the value associated with the cookie.
Again, the name and the value are the two parts of a cookie that you almost
always care about, although in a few cases a name is used as a boolean flag, and
its value is ignored (i.e the existence of the name means true).
getVersion/setVersion: Used to set or get version of the cookie,
respectively.
Advantages and Disadvantages:
Advantages:
- Reduced network traffic as compared to URL
rewritting.
- Used to maintain the data for a client.
- Reduced the application logic complexity.
Disadvantages:
- Cookies are not secured
- These are http specific thus it can be
used for http request only.
- A client has option to disable cookies.
Example: In this application I describe
how to add and retrieve cookies to maintain client data. This application has four files:
Index.jsp:
<html>
<body bgcolor="skyblue">
<form action="AddCokieServlet.java"><pre>
Name:<input type="text" name="cname"/><br/>
Value:<input type="text" name="cvalue"/><br/>
<input type="submit" value="Add
Cookie"/>
</pre> </form>
</body>
</html>
AddCookieServlet.java:
package my;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddCokieServlet
extends HttpServlet
{
public void service(HttpServletRequest req,
HttpServletResponse res)throws ServletException,
IOException
{
String name=req.getParameter("cname");
String value=req.getParameter("cvalue");
Cookie c=new
Cookie(name,value);
res.addCookie(c);
res.setContentType("text/html");
PrintWriter out =
res.getWriter();
String
output="<html><body>";
output+="Cookie
Added Successfully <br/>";
output+="<a
href='index.jsp'>Add one More <br/>";
output+="<a
href='viewcookies'>View Cookies <br/>";
output+="</html></body>";
out.println(output);
}
}
GetCookieServlet.java:
package my;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetCokieServlet
extends HttpServlet
{
public void service(HttpServletRequest req,
HttpServletResponse res)throws ServletException,
IOException
{
res.setContentType("text/html;charset=UTF-8");
PrintWriter out = res.getWriter();
out.println("<html><body><table
border=1>");
out.println("<tr><th>Name</th><th>Value</></tr>");
Cookie[] c=req.getCookies();
if(c!=null)
{
for(int i=0;i<c.length;i++)
{
out.println("<tr><td>"+c[i].getName()+"</td>");
out.println("<tr><td>"+c[i].getValue()+"</td>");
}
}
out.println("</table><br/><br/>");
out.println("<a href='index.jsp'>Add
One More</a><br/>");
out.println("</body></html>");
}
}
web.xml:
<?xml version="1.0"
encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>AddCokieServlet</servlet-name>
<servlet-class>my.AddCokieServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>GetCookieServlet</servlet-name>
<servlet-class>my.GetCookieServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AddCokieServlet</servlet-name>
<url-pattern>/AddCokieServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetCookieServlet</servlet-name>
<url-pattern>/viewcookies</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
After that we run this application on Server the outputs of this
application is given below:
Index.jsp:
AddCookieServlet.java:
GetCookieServlet.java: