- Java EE Tutorials
- JSP Tutorials
- Recommended Books for Java Server Pages (JSP)
Cookie is simple information stored by server on client side. Some of the features of cookie are:
- It allows servers to store user’s information on user machine. Cookies are stored in the partition assigned to the web server’s domain.
- All cookies for that domain are sent in every request to that web server. Cookies help in to identify user uniquely for session management.
- Cookies have lifespan and flushed by the client browser at the end of that lifespan. The purpose of cookie is to recognize user on server.
We can create cookies using Cookie class in the servlet API. We can add cookies to response object using addCookie () method. We can access cookies using getCookies () method. For Example: If we want to store user’s name on our web site then we can use following simple code to store that cookie.
String name=request.getParameter (“username”); Cookie c=new Cookie (“yourname”, name); response.addCookie(c);
Cookie working
- We can use HTTP cookies to perform session management. The web container stores the session ID on the client machine. When the getSession () method is called, the web container uses session ID cookie information to find the session object.
- The cookie mechanism is the default HttpSession strategy. Some browsers do not support cookies or some users turn off cookies on their browsers. If that happens then cookie based session management fails.
- We can detect if cookies are being used for session management using isRequestSessionIdFromCookie method in the HttpServletRequest interface. It returns true if cookies are used.
- A similar method isRequestSessionIdFromURL method returns true if URL-rewriting is used for session management.
Cookie class has following methods:
- getComment () : Returns comment describing the purpose of the cookie.
- getMaxAge () : Returns maximum specified age of the cookie.
- getName() : Returns name of the cookie.
- getPath() : Returns URL for which cookie is targeted.
- gatValue() :Returns value of the cookie.
- setComment(String) : Cookie’s purpose will be described using this comment.
- setMaxAge(int) : Sets maximum age of the cookie. A zero value causes the cookie to be deleted.
- setPath(String) : It determines Cookie should begin with this URL .
- setValue(String) : Sets the value of the cookie.
Cookie Example
First we will create html file as cookie1.html. In this html file we are using <form> tag with method and action attributes. In method attribute we are defining HTTP method called post which do not stores requests in the browser history and action tag will redirect to another JSP file called cookie2.jsp.
Listing 1:cookie1.html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Cooking Tracking Example</title> </head> <body> <form method = "post" action="cookie2.jsp"> User Name : <input type = "text" name = "uname"> input type="submit" value="submit" > </form> </body> </html>
Now create JSP file as cookie2.jsp.This file is what us referred in cookie1.html file. In this JSP file we are using cookie object. Writing data to cookie object is done while loading new page. Means when submit button is pressed in cookie1.html page it would store value in a cookie.
Listing 2:cookie2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String name=request.getParameter("uname"); Cookie cookie = new Cookie ("uname",name); response.addCookie(cookie); cookie.setMaxAge(50 * 50); //Time is in Minutes %> <p>Display the value of the Cookie</p> User Name is :<%= request.getParameter("uname")%> </body> </html>
Execute the jsp file cookies1.htm in Eclipse by selecting Run As > Run On Server an output as below would be seen:
Now enter “javabeat” and click on submit button:
Previous Tutorial : JSP Expression Language : Next Tutorial : Exception Handling in JSP