• Cookie is a class available in the javax.servlet.http package.
• A cookie is simple information with a name and value.
• The name and value of the cookie will be of the string type.
• Normally, cookies are created on the server machine and persist or are stored on the client machine.
• Cookies created at server Mechanic will come to client Mechanic along with HttpResponse.
• Cookies persisted on the client machine will go to the server machine along with HttpRequest.
Cookie ck=new Cookie(“email”,som@jtc.com);
Adding cookies to response
Response.addCookie(ck);
Accessing cookies from request
Cookie ck[]=request.getCookies();
For(cookie c:ck){
String cn=c.getName();
String cv=c.getValue();
System.out.println(cn+”
+cv);
}
By default, the web container will do the following regarding session management:
• Created one special cookie with
o JSESSIONID as Cookie value.
o Session as cookie value.
▪ Cookie ck=new Cookie(“JSESSIONID”,ses.getId());
• Adds that cookie to the response.
o Response.addCookie(ck);
• Collects the special cookies and identifies the session object based on the session ID collected from the special cookies.
o Code…….
• Assume that sessionMap is the map object that contains key and value.
Key will be SESSIONID
Value will be SessionObject
• Use the following options to remove the cookies stored on the client machine:
o You need to specify the maximum age of cookies as 0.
o You need to add the same cookie to the response.
Cookie c=……..;
c.setMaxAge(0);
res.addCookie(c);
code……..
String bnm=request.getParameter(“bname”);
Cookie ck[]=request.getCookies();
for(Cookie c:cs){
c.setMaxAge(0);
res.addCookie(c);
}
}
File Required For Program
1. Index.html
2. AddServlet.java
3. RemoveServlet.java
4. Web.xml
1. Index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Cookie Example</h1>
<form action="add.jtc" method="post">
<input type="text" name="bname"/><br>
<input type="submit" value="ADD"/>
</form>
<hr>
<form action="remove.jtc"
method="post">
<h2>Enter Book Name</h2>
<input type="text" name="bname"/>
<br><input type="submit"
value="Remove"/>
</form>
</body>
</html>
2. AddServlet.java
package com.jtcindia.servlet;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AddServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Writer out = res.getWriter();
String bnm = req.getParameter("bname");
Cookie c1 = new Cookie(bnm, null);
res.addCookie(c1);
out.write("<h1>" + bnm + " " + bnm);
Cookie cs[] = req.getCookies();
if (cs == null) {
out.write("<h2>You are new client");
HttpSession sess = req.getSession();
} else {
boolean found = false;
for (Cookie c: cs) {
String nm = c.getName();
String val = c.getValue();
out.write("<h2>" + nm + ":" + val);
if (nm.equals("JESSIONID")) found = true;
}
if (found) {
out.write("<h2>you are old client");
HttpSession sess = req.getSession();
}
}
out.write("<hr>");
RequestDispatcher rd = req.getRequestDispatcher("index.html");
rd.include(req, res);
}
}
3. RemoveServlet.java
package com.jtcindia.servlet;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RemoveServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException
IOException {
String bnm = req.getParameter("bname");
Writer out = res.getWriter();
Cookie cs[] = req.getCookies();
if (cs == null) {
out.write("<h2>You are new client");
HttpSession sess = req.getSession();
} else {
boolean found = false;
for (Cookie c: cs) {
String nm = c.getName();
String val = c.getValue();
if (nm.equals("JESSIONID")) {
found = true;
out.write("<h2>" + nm + ":" + val);
} else if (nm.equals(bnm)) {
c.setMaxAge(0);
res.addCookie(c);
} else {
out.write("<h2>" + nm + ":" + val);
}
if (found) {
out.write("<h2>you are old
client ");
}
else {
out.write("<h2>you are New
client ");
HttpSession sess = req.getSession();
}
}
out.write("<hr>");
RequestDispatcher
rd = req.getRequestDispatcher("index.html");
rd.include(req, res);
}
}
}
4. Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0">
<display-name>Jtc22</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AddServlet</servlet-name>
<servlet-class>com.jtcindia.servlet.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddServlet</servlet-name>
<url-pattern>/add.jtc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>RemoveServlet</servlet-name>
<servlet-class>com.jtcindia.servlet.RemoveServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RemoveServlet</servlet-name>
<url-pattern>/remove.jtc</url-pattern>
</servlet-mapping>
</web-app>
• Container uses session ID to identify the client as old or new.
• Container sends the session ID to the client machine as a cookie with the name JSESSIONID.
• Sometimes, you may have problems with cookies.
o When your browser is not supporting cookies.
o When the client deletes the cookies.
• When any problem occurs with cookies, the request will not carry the cookie with the name JSESSIONID.
• If a request is coming without a session ID cookie, then the container will treat that client as new and provide the new session object and new sessionid, i.e., the client is losing the previous session object and conversational data available in that session object.
• As an alternative, you can use URLs or hidden files to carry the session ID from client to server and from server to client.
• URL Rewriting is the process of attaching the session ID to the URL. It is also called the encoding of the URL.
• You can use the following method with a response object to encode the URL:
o String url=response.encodeURL("hello.jsp");
url will be hello.jsp;jsessionid=A12jkd6d57 sdsdsd
• The encodingURL() method takes the URL as a parameter and encodes the URL.
• encodeURL() is deprecated.
• You can store the session ID in the hidden fields as follows:
o <input type=”hidden” name=”JSESSION” value=”<%=session.getId()%>”>
• You can develop the web application using the following:
o Servlet and jsp
o Struts1
o Struts 2
o Jsf
o Spring MVC
• When you develop the web application using Servlets and JSP, you are responsible for the implemented URL Rewriting.
• When you develop the web application using web frameworks, you are not responsible for implementing URL Rewriting is necessary because every web framework has built-in support for URL Rewriting.