Followers

Servlet: Session tracking using Cookies

The following example demonstrates how to use this cookies session tracking in servlet application.

1. In this cookies session tracking, the data of every individual client is dealing back to the client browser in the form of objects of Cookies class so that the client browser will receive those Cookies maintained at the client side temporarily.
2. When the same client makes another request to the application then the client browser will automatically send all the cookies along with the request.


index.html
=========

<form action="firstServlet" method="post">
Name:<input type="text" name="userName"/><br/>
Address :<input type="text" name="address"/><br/>
<input type="submit" value="submit"/>
</form>

web.xml
=======
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>FirstServlet</servlet-name>
        <servlet-class>FirstServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>SecondServlet</servlet-name>
        <servlet-class>SecondServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FirstServlet</servlet-name>
        <url-pattern>/firstServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>SecondServlet</servlet-name>
        <url-pattern>/secondServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

The URL pattern of the class must be /firstServlet, this will receive the form request with 3 parameters userName, address and submit as getRequest.

FirstServlet.java
===========

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class FirstServlet extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
    
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
        
    String name=request.getParameter("userName");
    String addr=request.getParameter("address");
    out.print("Hello "+name);
    out.println("<br /><br />");
    out.print("Address "+addr);
    out.println("<br /><br />");
  
    Cookie ck1=new Cookie("uname",name);//creating first cookie object
          /*
          This will create an object of Cookie class along with name value of the client with attribute name as "uname"       
          */
    response.addCookie(ck1);//adding first cookie in the response
          /*
          Where addCookie() is the method of HttpServletResponse interface that will add the respective Cookie to outer response object.
          So the signature of the method is,
          public void addCookie(Cookie ck);
          */
    Cookie ck2=new Cookie("uaddr",addr);//creating second cookie object
          /*
          This will create another object of Cookie class along with address value of the client with attribute name as "uaddr"    
          */
    response.addCookie(ck2);//adding second cookie in the response

    //creating submit button
    out.print("<form action='secondServlet'>");
    out.print("<input type='submit' value='submit'>");
    out.print("</form>");
        
    out.close();
        
  }
}
The URL pattern of the class must be /SecondServlet, this will receive the request with 2 Cookies without any parameters when client click the anchoring message.

SecondServlet.java
=============

import java.io.*; 
import javax.servlet.ServletException;
import javax.servlet.http.*;  
public class SecondServlet extends HttpServlet {   
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{   
    response.setContentType("text/html"); 
    PrintWriter out = response.getWriter();       
    Cookie ck[]=request.getCookies(); 
          /*
          Where getCookies() is the method of HttpServletRequest interface that will read all the Cookies from the request object & return an array of references to those Cookies . So the signature of the method is.
          public Cookie[] getCookies();        
          */
         
    out.print("Hello "+ck[0].getValue()); 
    out.println("<br /><br />");
    out.print(ck[1].getName()+" Hi "+ck[1].getValue()); 
          /*
          Where getName() is the method of Cookie class that will return the attribute name from the Cookie object as a String.
          The signature of the method is.
          public String getName();
         
          Where getValue() is the method of Cookie class that will return the attribute value from the Cookie object as a String.
          The signature of the method is.
          public String getValue();
          */
    out.close(); 
    }

}   

No comments:

Post a Comment