URL Rewriting - JSP

URL rewriting is essentially the rewriting of every URL contained within a Web page being delivered by your server. Take the following basic HTML examples.

Assuming cookies are disabled in the browsers, as users click through each of these pages, there is no way of differentiating one user from another. If, however, these pages were dynamically modified for each user with all URLs now containing a unique identifier, then whenever a user clicks a link, this unique identifier can be extracted to identify the user when the request reaches the server. To encode a URL with the session identifier, there is a method in the HttpServletResponse object called encodeURL(String url).This method takes in a String to represent the URL to be encoded, and then adds the session identifier to the end of the URL. Here is page1.html rewritten as a JSP with the URL encoded:

When this page is viewed in a browser, selecting View/Source from the menu reveals what has happened to the URL:

Note that the URL now has the session ID on the end.This can be used to identify the current session on the server. URL rewriting is not as sophisticated as using cookies. Here are some of the limitations of using URL rewriting:

  • It does not support storing name/value pairs on the client like cookies can.
  • It does not support the persistence of a session after a browser shutdown like persistent cookies can.
  • You lose the session if only one URL has failed to be rewritten, or if the user manually enters a URL to one of your pages.

The key benefit of URL rewriting is that it works in every browser! The rewriting of the URLs happens on the server, so any client application can be used. So, given these two alternative approaches to session tracking, which should you use? In summary, the best approach is to use cookies if possible, but default to URL rewriting if cookies are not supported. If you are using an application server, this may well be taken care of for you.You will need to modify a configuration somewhere.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JSP Topics