Session Management Using URL Rewritting Instead of Cookies

Introduction

 
This article is next in the series of articles about Java Servlet Session management. In this article, we will learn about maintaining the client state or session by using URL Rewriting in a Servlet.
 
A Web container can use several methods to associate a session with a user, all of which involve ing an identifier between the client and server. The identifier can be maintained on the client as a cookie or the Web component can include the identifier in every URL that is returned to the client.
 

Session Management Using URL Rewriting Instead of Cookies

 
In some situations, a browser or wireless device may not accept cookies, which makes session tracking with cookies impossible. URL rewriting provides you with another session tracking alternative. that can be substituted automatically when Server detects that the browser does not accept cookies. URL rewriting involves encoding the session ID into the hyperlinks on the Web pages that your servlet sends back to the browser. When the user subsequently clicks these links, Server extracts the ID from the URL address and finds the appropriate HttpSession when your servlet calls the getSession() method.
 
If your application makes use of session objects, you must ensure that session tracking is enabled by having the application rewrite URLs whenever the client turns off cookies. You do this by calling the response's encodeURL(URL) method on all URLs returned by a servlet. This method includes the session ID in the URL only if cookies are disabled; otherwise, it returns the URL unchanged.
 
Usually, this technique is used when information that is to be transferred is not very critical because the URL can be intercepted easily during transfer. Given below the example of URL rewriting :
 
UrlRewritingServ.JAVA
  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3. import java.util.Date;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. import javax.servlet.http.HttpSession;  
  9. public class UrlRewritingServ extends HttpServlet {  
  10.     static final String COUNTER_KEY = "Counter.count";  
  11.     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
  12.         HttpSession session = req.getSession(true);  
  13.         resp.setContentType("text/html");  
  14.         PrintWriter out = resp.getWriter();  
  15.         int count = 1;  
  16.         String str = "Url rewriting";  
  17.         Integer i = (Integer) session.getAttribute(COUNTER_KEY);  
  18.         if (i != null) {  
  19.             count = i.intValue() + 1;  
  20.         }  
  21.         session.setAttribute(COUNTER_KEY, new Integer(count));  
  22.         out.println("<BODY BGCOLOR=\"yellow\">\n" +  
  23.             "<H1 ALIGN=\"CENTER\">" + str + "</H1>\n" +  
  24.             "<H2>Information about Your Session:</H2>\n" +  
  25.             "<TABLE BORDER=1 ALIGN=CENTER>\n" +  
  26.             "<TR BGCOLOR=\"yellow\">\n" +  
  27.             "  <TH>Info Type<TH>Value\n" +  
  28.             "<TR>\n" +  
  29.             "  <TD>Session ID\n" +  
  30.             "  <TD>" + session.getId() + "\n" +  
  31.             "<TR>\n" +  
  32.             "  <TD>Creation Time\n" +  
  33.             "  <TD>" + new Date(session.getCreationTime()) + "\n" +  
  34.             "<TR>\n" +  
  35.             "  <TD>Time of Last Access\n" +  
  36.             "  <TD>" + new Date(session.getLastAccessedTime()) + "\n" +  
  37.             "<TR>\n" +  
  38.             "  <TD>Time out\n" +  
  39.             "  <TD>" + new Date(session.getMaxInactiveInterval()) + "\n" +  
  40.             "<TR>\n" +  
  41.             "</TABLE>\n" +  
  42.             "</BODY></HTML>");  
  43.         out.println("</b> Number of Previous Accesses of this page <b>" + count + "</b> time(s) during this browser session");  
  44.         String url = req.getRequestURI();  
  45.         out.println("<form method=GET action=\"" + resp.encodeURL(url) + "\">");  
  46.         out.println("<input type=submit " + "value=\"Hit page again\">");  
  47.         out.println("</form>");  
  48.         out.flush();  
  49.     }  
  50.     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
  51.         doGet(req, resp);  
  52.     }  
  53. }  
web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"  
  3. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  4. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  5.  <servlet>  
  6.     <servlet-name>UrlRewritingServ</servlet-name>  
  7.     <servlet-class>UrlRewritingServ</servlet-class>  
  8.   </servlet>  
  9.   <servlet-mapping>  
  10.     <servlet-name>UrlRewritingServ</servlet-name>  
  11.     <url-pattern>/UrlRewritingServ</url-pattern>  
  12.   </servlet-mapping>  
  13. </web-app>  
when you run this servlet the output is as follows: 
 
URL rewriting Ist
 
urlRewriting1st.gif
 
URL rewriting  2nd
 
urlRewriting2nd.gif
 
I think this article will be helpful to learn about Session management by URL Rewriting in JAVA.