Servlet Cookies in Session Tracking

public class Cookie
A Cookie is a small piece of textual information sent by the server to the client , stored on the client, are returened by the client for all requests to the server.
A Cookie contains one or more name-value pairs with certain additonal attributes, which are exchanged in the response and request headers.

Some important Method of Cookie Class

  1. public int getMaxAge()
              This method returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until browser shutdown.  
  2. public String getName()
              This method returns the name of the cookie.  
  3. public String getPath()
              This method returns the path on the server to which the browser returns this cookie.  
  4. public String getValue()
              This method returns the value of the cookie.  
  5. public void setMaxAge(int expiry)
              This method sets the maximum age of the cookie in seconds.  
  6. public void setValue( String newValue)
              This method assigns a new value to a cookie after the cookie is created.  
Other methods required for using Cookies
For adding cookie or getting the value from the cookie, we need some methods provided by other interfaces. They are:
  1. public void addCookie(Cookie cookiesName):
               This is method of HttpServletResponse interface and it is used to add cookie in response object.
  2. public Cookie[] getCookies():
              This is method of HttpServletRequest interface and it is used to return all the cookies from the browser.

Example of Session Tracking using Cookie class

index.html
<html> <head> <title>Cookie Demo Servlet </title> </head> <body> <form action="FirstServlet" method="post"> User Name <input type="text" name="userName"> &nbsp; <input type="submit" value="Submit"> <br> </form> </body> </html>
FirstServlet.java
import java.io.PrintWriter; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String uname=request.getParameter("userName"); out.print("Welcome "+uname); //Creating Cookie Object Cookie cookie=new Cookie("uname",uname); //Adding Cookie object in the response response.addCookie(cookie); out.print("<form action='SecondServlet' method='post'>"); out.print("<input type='submit' value='Login'>"); out.print("</form>"); out.close(); } catch(Exception e){System.out.println(e);} } }
SecondServlet.java
	
import java.io.PrintWriter; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SecondServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); Cookie cookie[]=request.getCookies(); out.print("Cookies Value "+cookie[0].getValue()); out.close(); } catch(Exception e){ System.out.println(e); } } }
web.xml
	
<?xml version="1.0" encoding="UTF-8"?> <web-app > <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>SecondServlet</servlet-name> <servlet-class>SecondServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SecondServlet</servlet-name> <url-pattern>/SecondServlet</url-pattern> </servlet-mapping> </web-app>
Output
cookie
cookie
Download this example
1) Deployed using Tomcat 6.0
2) Deployed using eclipse IDE
 



 

Latest Tutorial


Most Visited Tutorial