Page 1 of 1

Servets Maintaining sessions using URL rewriting Rate Topic: -----

#1 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Posted 07 March 2008 - 07:59 PM

Attached File  tutorial_2.zip (2.71K)
Number of downloads: 401Hi,

I will be using the basic example from the tutorial here

The code is reproduced here:
Note: There is no change in web.xml

public class CheckServlet extends HttpServlet {
    static int count=0;
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    public void service (HttpServletRequest request,HttpServletResponse response) throws                        IOException {
        PrintWriter writer = response.getWriter();
        writer.println(++count);
        writer.println("The name as specified in web.xml :" + getServletName() );
    }
}



The count in above code is static and hence every user sees incremented value when he hits the servlte. But now we want that the requests coming from a particular browser be incremented from 0 onwards. Any request coming from some other browser will get an initial value of count as 0.
The code for that is:

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


public class CheckServlet extends HttpServlet {

    public void init(ServletConfig config) throws ServletException  {
       super.init(config);
    }

     public void service (HttpServletRequest request,HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        HttpSession session = request.getSession(true);
        Integer iCount = (Integer)session.getAttribute("count");
        if(iCount == null) {
            iCount = new Integer(0);
            session.setAttribute("count",iCount );
        } else {
            iCount = new Integer(iCount.intValue() + 1);
            session.setAttribute("count",iCount );
        }

        PrintWriter writer = response.getWriter();
        writer.println("<html>");
        writer.println("<title>Welcome</title>");
        writer.println("<body>");
        writer.println("The name as specified in web.xml :" + getServletName());
        writer.println("count is :" + iCount);
        writer.println("</body>");
        writer.println("</html>");
    }
}



The most important line in this program is :
HttpSession session = request.getSession(true);

The above line gets a session object from request sent by the browser. The very first request doesn't has any but then the server will insert one in its first response to browser because of the parameter "true" bein sent to getSession method.
The session object can store attributes with some associated values with it. First time as the session is not present the vakue of the attribute will be null as it won't be present. In that case, we insert a count (initialized to 0) in the session object and in every subsequent object we keep on incrementing the count (that shows something is preserved by the browser and server).
Now if the servlet is hit some other browser, the initial count will always be 0 for the first request.

The session object is identified by the server by the session id. In fact, when a session is associated to first response, a parameter named as jSessionID is also appended to the URL and the browser sends the same session id back to server on each subsequent request which helps the server to recognize its clients.

This approach is known as URL rewriting because of the session id being appended to the URL.

The whole web application has been attached as zip file alongwith. You can extract its contents into your webapps directory and try this example.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1