Friday, January 29, 2010

URL rewriting

Both of these methods revolve around the concept of URL rewriting. Basically it involves suffixing the URL with a HTTP_QUERY_STRING, which mostly is a session id used to keep track of a particular users context.

URL re-writing is used get over the usage of cookies which are dependent on browser configuration, thus not reliable at all.

The difference between the methods is in their usage and not their functionality. Both perform the same task of re-writing a URL.

encodeRedirectURL
When you send a redirect header to the browser using response.sendRedirect(string URL)

Example, you want to redirect to different page.

if (name == null){
  response.sendRedirect(response(encodeRedirectURL("errorPage.jsp"))
}


encodeURL
On the other hand, you provide a link to shopping cart page to a user.

printWriter.println("A HREF=" + response.encodeURL("shoppingCart.jasp") + ">")


Essentially they do same thing of re-writing the URl for non-cookie compliant browser.

encodeURL() is used for all URLs in a servlet's output. It helps session ids to be encoded with the URL.

encodeRedirectURL() is used with res.sendRedirect only.It is also used for encoding session ids with URL but only while redirecting.

No comments: