Managing Session using Cookies
Cookies are small bits of textual information that a web server sends to a browser and that browser returns the cookie when it visits the same site again. In cookie the information is stored in the form of a name, value pair. By default the cookie is generated. If the user doesn’t want to use cookies then he/she can disable them.
The class javax.servlet.http.Cookie, represents the cookie. HttpservletRequest and HttpServletResponse interfaces have methods for getting and setting the cookies. We can create cookie, read cookie and send cookie to the client browser. We can create cookie by calling the Cookie Constructor. To send cookie, a Servlet creates cookie and add the cookie to the response header with method response.addCookie(cookie). To read cookies, we have to call request.getCookies() method which returns an array of Cookie objects. The example codes are placed within CookieExample directory under webapps.
All the codes are given below one-by-one.
k.html (HTML Form for data submission)
AddCookieServlet.java [Servlet code for Creating and Sending Cookie]
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AddCookieServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Get parameter from HTTP request
String data = request.getParameter("data");
//Create cookie
Cookie cookie = new Cookie("techguru", data);
//Add cookie to HTTP response
response.addCookie(cookie);
//Write output to browser
PrintWriter pw = response.getWriter();
pw.println("MyCookie has been set to");
pw.println(data);
pw.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
GetCookiesServlet.java [Servlet code for Retrieving Cookie]
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetCookiesServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Get cookies from header of HTTP request
Cookie[] cookies = request.getCookies();
//Display these cookies
PrintWriter pw = response.getWriter();
pw.println("");
for(int i = 0; i < cookies.length; i++) {
String name = cookies[i].getName();
String value = cookies[i].getValue();
pw.println("name = " + name + "; value = " + value);
}
pw.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
The web.xml file for this web application:
<web-app>
<servlet>
<servlet-name>amt</servlet-name>
<servlet-class>AddCookieServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>amt1</servlet-name>
<servlet-class>GetCookiesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>amt</servlet-name>
<url-pattern>/AddCookieServlet.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>amt1</servlet-name>
<url-pattern>/GetCookiesServlet.do</url-pattern>
</servlet-mapping>
</web-app>
The first run of the program is given below:
On clicking the “Submit button” will produce the following output:
In order to retrieve the cookie value, we have to type the following link in the address bar:
http://localhost:8080/CookieExample/GetCookiesServlet.do
Now see the following output.