Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Session Handling: URL Rewriting

URL rewriting is another way of implementing session handling. In this technique a unique session identifier is generated and attached to each and every URL sent to the client browser. For example, if any HTML page is sent to the client browser, any URLs in that page will contain the session identifier. For every request from the client browser, the servlet checks for this session identifier. If this identifier is missing in the URL, the servlet treats it as a new session and generates another session identifier.

WebLogic Server provides an implementation of the encodeURL() method for the response object that generates and appends the session ID to the URL being written to the browser. URL links in any HTML page that the servlet generates need to be encoded with the encodeURL() method and not be written directly. A code snippet would be

out.println("<a href=\"" + response.encodeURL("/servlet/myServlet") + "\">
    Click here!</a>");

The encodeURL method appends the session ID to this link in the generated HTML code. Hence, the HTML page contains the following HTML code:

<a href="/servlet/myServlet/sessionID=12345678">Click here!</a>

WebLogic Server provides support for URL rewriting by handling the generation of the unique session ID and appending to URLs in your application. However, since each and every URL in your application must be encoded to add the session identifier, this is not a foolproof method. Also, this method is cumbersome for developers to incorporate.

Sample Program

Now you will look at a sample program (Listing 3.2) that implements this session- handling technique. Figure 3.15 shows the sequence diagram for this program.

Figure 3.15 Sequence diagram for session handling using URL rewriting.

Listing 3.2 BookShoppingServlet.java

 * Class Name:BookShoppingServlet
 * Extends:HttpServlet
 * Description:Shopping Cart Application Using url rewriting,
 * @author Mandar S. Chitnis, Lakshmi AM.    @version 1.1
 * Copyright (c) by Sams Publishing. All Rights Reserved.
package com.sams.learnweblogic7.servlets;

//import the utils package
import com.sams.learnweblogic7.utils.*;

//importing standard servlet packages
import javax.servlet.*;
import javax.servlet.http.*;

//import io,util, and math packages
import java.io.*;
import java.util.*;
import java.math.*;

public class BookShoppingServlet extends HttpServlet

  //declaring global variables
  Book book1;
  Book book2;
   Book book3;

  PrintWriter out;

  private static final int DEFAULT_ZERO_VALUE = 0;
  private static final String EXAMPLE_TYPE
= "Shopping Cart Using URL Rewriting";
  public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
    out = res.getWriter();
    writeHeaderZone(out, EXAMPLE_TYPE);
    doSessionUsingURLRewriting(out, req, res);
  }//end of doPost

  public void doSessionUsingURLRewriting(PrintWriter outputObject,
HttpServletRequest req, HttpServletResponse res){
    String clickedButton
= (req.getParameter("buttonName") == null)?"":req.getParameter("buttonName");
      outputObject.println("<FORM name=\"viewcart\"
          action=\"/ShoppingApp/BookShoppingServlet\" method=\"get\">");
      outputObject.println("<B><font face = \"Verdana\" color=\"blue\"
          -size=-2><p align = \"right\">Shopping Cart using URL  Rewriting</p>
      int book1Qty
= Utility.getDefaultValue(req.getParameter("Book1Qty"),0);
      int book2Qty
= Utility.getDefaultValue(req.getParameter("Book2Qty"),0);
      int book3Qty
= Utility.getDefaultValue(req.getParameter("Book3Qty"),0);
      writeViewCartZone(outputObject,book1Qty, book2Qty, book3Qty);
      outputObject.println("<p align =\"center\"><INPUT type=\"submit\"
          name=\"buttonName\" value=\"Empty Shopping Cart\"></p>");
      rewriteURL(outputObject, req, res);
      outputObject.println("<B><font face = \"Verdana\" color=\"blue\"
          -size=-2><p align = \"right\">Shopping Cart using URL  Rewriting</p>
      outputObject.println("<CENTER><TABLE width = 100%><TR>");
      outputObject.println("<TD><INPUT type=\"submit\"
          name=\"buttonName\" value=\"add\"></TD>");
      outputObject.println("<TD><INPUT type=\"submit\"
          name=\"buttonName\" value=\"view\"></TD>");

  public void rewriteURL(PrintWriter outputObject, HttpServletRequest req,
      HttpServletResponse res){
    int Book1Qty = Utility.getDefaultValue(req.getParameter("Book1Qty"),0)+
    int Book2Qty = Utility.getDefaultValue(req.getParameter("Book2Qty"),0)+
    int Book3Qty = Utility.getDefaultValue(req.getParameter("Book3Qty"),0)+
    -String reWrittenURL = res.encodeURL("/ShoppingApp/
BookShoppingServlet?Book1Qty="+Book1Qty+" &Book2Qty="+Book2Qty+"&Book3Qty="+Book3Qty); outputObject.println("<FORM name=\"addcart\" action="+reWrittenURL+" method=\"post\">"); } ... }//end of BookShoppingServlet

If you look at the servlet program, the only difference in the code is the addition of the doSessionUsingURLRewriting() method, which performs the session handling for the servlet. This method is called from the doPost() method.

The doSessionUsingURLRewriting() method retrieves the existing contents of the shopping cart using the getParameter() method of the request object. Also, note that to demonstrate the appending of the session ID in URL rewriting, the HTML form's method was changed to GET instead of the default POST method. This enables you to look at the session ID appended to the URL.

To determine whether the user action is Add To Cart or View Cart, retrieve the value of the buttonName parameter from the request. This is similar to what was done for the previous session-handling techniques:

outputObject.println("<FORM name=\"viewcart\"
    action=\"/ShoppingApp/BookShoppingServlet\" method=\"get\">");

For the Add To Cart function, you need to retrieve the contents of the shopping cart as well as the new user selections. This is done by calling the rewriteURL() method. The shopping cart contents are retrieved by calling the getParameter() method with the parameter name as Book1Qty. The user selection is retrieved by calling the getParameter() method with the parameter name as book1_qty. These new shopping cart contents are then calculated:

int Book1Qty = Utility.getDefaultValue(req.getParameter("Book1Qty"),0)+

Similar calculations are done for the other books in your store.

Since the HTML form in the book-listing page is now sending the form data in a GET request instead of a POST request, you have to modify the action URL and append the shopping cart contents (updated with the user selection). This form-action URL is generated using the encodeURL() method, thus enabling the WebLogic Server to append the session ID to the URL:

String reWrittenURL = res.encodeURL("/ShoppingApp/BookShoppingServlet?

Finally, send the book-listing page back to the browser.

For the View Cart function, you need to retrieve the contents of the shopping cart stored in the browser. This is done by the following call:

int book1Qty = Utility.getDefaultValue(req.getParameter("Book1Qty"),0);

This is similar to the retrieval of shopping contents for the Add To Cart function. The only difference is that there will be no new user selections and hence no updates required to the shopping cart data. You will perform similar actions for the rest of the books in your store.

Finally, generate the View Cart page using the writeViewCartZone() method with the retrieved shopping cart data as parameters.

To keep the application simple, no functionality for emptying or removing the contents of the shopping cart is provided.

Compile the Program

Since you are not adding any new classes nor are you changing the deployment location of the application, the compiling of the servlet program is similar to compiling performed in the hidden-variables session-handling method.

Deploy the Program

You need to edit the WebLogic Server–specific deployment descriptor file weblogic.xml to enable the settings for URL rewriting. To do this, set the following tags:


Execute the Program

The book-listing page is invoked from the browser by calling the following URL:

  • + Share This
  • 🔖 Save To Your Account