Monday, June 11, 2012

encodeURL vs. encodeRedirectURL in Java?

 Both these methods revolve around the concept of URL rewriting. I don’t know whether u r aware bout this. But basically it involves suffixing the URL with aHTTP_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 dependenton browser configuration, thus not reliable at all. Now Session is a comprehensive topicon its own, so won’t discuss it now.But yes the difference between the methods is in their usage and not their functionality.Both perform the same task of re-writing a use encodeRedirectURL when u send a redirect header to the browser usingresponse.sendRedirect(string URL)for e.g. lets say on a particular condition u want to redirect to different pageif (name == null){response.sendRedirect(response(encodeRedirectURL("errorPage.jsp"))}on the other hand lets say u want to provide a link to shopping cart page to a user - printWriter.println("A HREF=" + response.encodeURL("shoppingCart.jasp") + ">")thus essentially they do same thing of re-writing the URL for non-cookie compliant browser.In short.....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 encodingsession ids with URL but only while redirecting.

No comments:

Post a Comment