Friday 8 August 2014

Lets have a look at Session Tracking.


Here we would be discussing about how to track session in java.
But before moving on to Session Tracking, we must know something about Servlets. Here are some key points. With the request to servlet,

·         4 Objects are made, HttpServletRequest, HttpServletResponse, Document and  PrintWriter (one of             each, at the time of request to servlet)
·         Name/Value pair, which is extracted from request is kept in object of HttpServletRequest  .
·         Specified Servlet is searched. Here story may end (if class is not found), Error message will be kept in           response.
·         Pointer of HttpServlet is taken, Object of specified servlet is made, and its address is assigned to pointer.       Again, story may end (if class do not extend HttpServlet or it is not public)
·         Now doGet(HttpServletRequest,HttpServletResponse) method is called using that pointer. For execution of doPost method, explicit call need to be placed, by default, doGet is called.

After execution of doGet, whatever kept in object of document will be sent to client( browser).

Here's an example for download (lets say one.com)
https://docs.google.com/uc?export=download&id=0B0p0u1WTbGiRV0l1ZWxmMXpSbFE

Need of Session Tracking..???

ü        Problem: Data received in first request will not be available in second response.
Solution: Session Tracking i.e. If the data in the previous request is also available in current request, then it is said that session is being tracked.

How to Track Session...???

 Here we will be discussing on the following ways of Session Tracking.

  • Hidden Form Feed
  • URL Rewriting
  • Cookies
  • Session Object

1. Hidden Form Feed: 
        Data received in first request will be sent along with the response, but in a hidden manner. For doing so, we will keep the value of type attribute as hidden in input tag.
<input type="hidden" name='XYZ' value='Previous Data'>

Here's an example of Hidden Form Feed for download. (lets say two.com)
https://docs.google.com/uc?export=download&id=0B0p0u1WTbGiRQmk4WW5TYTBnb1E

2.URL Rewriting
      To make the data available in second request, we will keep it in query string. For this, we will have to programatically generate query string. But values in query string are encoded according to Http Protocols. Thus, we will be using encode method of class URLEncoder in the java.net package.
     Lets say, 'name' is the variable which is to be encoded.
name=URLEncoder.encode(name);

Here's an example of URL Rewriting for download. (lets say three.com)
https://docs.google.com/uc?export=download&id=0B0p0u1WTbGiROXBoQXZqdkQ0WDg

3. Cookies

  • Cookie is a particular type of object, in which name/value pair is stored.
  • Cookie is made at server side, and then kept in response so as to be transferred to browser
  • Browser will store the cookie at disk, and will also keep a track that from which site, the cookie came.
  • Next time when the request will be sent to that particular site, its cookies will also be sent along with the request.
    Cookie c1=new Cookie(“name”,java.net.URLEncoder.encode(name,”utf-8”));
    c1.setMaxAge(60*10); 
    c1.setPath("/");            

     When cookie is to be given to a particular resource, setPath() will be used 
     SetMaxAge() is used to specify life span of cookie.
     
    response.addCookie(c1);

    And at receiving end,
    c=request.getCookies();
    name=c[0].getValues();

Here's an example of Session Tracking through cookies for download. (lets say four.com)

4. Session Object
      Above listed all ways of tracking session are less reliable than session object. In this way,

   ü  Data in first request will be kept in a Session Object (which will be in RAM), and the second servlet             will get it from there.
   ü  Session Object will be requested from server, which will look that whether the object exists for this               connection or not.
   ü  If not, then address of a newly created session object will be given.
   ü  Session Object will not be shared by other connections and will not be destroyed after response.
   ü  By default, Max Time Interval: 30 min
   
             HttpSession hs=request.getSession();       à requested for session object.
      HttpSession s=request.getSession(false); àReference of existing object will be given, new                                                                                         will not be created( in case it do not exists).
     
       hs.setAttribute(“name”,name);
                                  name=(String)hs.getAttribute(“name”);   à At receiving end

              Here's an example of Session Tracking through Session Object for download (lets say five.com)

       Please leave comments.!!!

No comments:

Post a Comment