Thursday, January 19, 2012

Difference between URL-rewriting URL-encoding in Servlet JSP

URL-rewriting vs URL-encoding in Servlet JSP

Main difference between URL-rewriting and URL-encoding is that URL-rewriting is a technique to maintain user session if cookies are not enabled on client browser or browser doesn't support cookie while URL-encoding is a way to pass string to server containing special characters  by converting special characters like space into some other characters like + . people often confuse between URL encoding and URL rewriting because of there names which sounds quite similar for new guys but functionality wise they are totally different to each other, Also servlet encodeURL() method adds more confusion because its sounds like its used for URL Encoding but indeed used for URL Rewriting. This is also a very popular servlet and JSP interview questions , I have also shared some more questions on my posts 10 Servlet Interview questions answers and 10 JSP interview questions and answers for Java programmer.

Difference between URL Rewriting and URL Encoding in JSP Servlet

difference between URL-Rewriting and URL-Encoding in Java ServletYou often need to encode URL before sending it to server and need to rewrite URL with session id in order to maintain session where cookie is not present. here are some more differences between URL-rewriting and URL encoding in Servlet JSP

1) java.servlet.http.HttpServletResponse methods encodeURL(String url) and encodeRedirectURL(String URL) is used to encode SesssionID on URL to support URL-rewriting. don't confuse with name encodeURL() because it doesn't do URL encoding instead it embeds sessionID in url if necessary. logic to include sessionID is in method itself and it doesn't embed sessionID if browser supports cookies or session maintenance is not required. In order to implement a robust session tracking all URL from Servlet and JSP should have session id embedded on it.

In order to implement URL-rewriting in JSP you can use use JSTL core tag all URL passed to it will automatically be URL-rewriting if browser doesn't support cookies.

While and used to perform URL Encoding and decoding which replace special character from String to another character. This method uses default encoding of system and also deprecated instead of this you can use URL, String encoding) which allows you to specify encoding. as per W3C UTF-8 encoding should be used to encode URL in web application.

2) In URL rewriting session id is appended to URL and in case of URL-encoding special character replaced by another character.

That's all on difference between URL-rewriting and URL-encoding, let me know if you come across some more differences between these URL Encoding vs URL-rewriting in Servlet and JSP.

Further Learning
Java Web Fundamentals By Kevin Jones
Introduction to Spring MVC 4
Head First Servlet and JSP

Some more Java post you may like


Anonymous said...

is there any to encrypt URL which we pass through HTTP GET Request, I want that nobody sees the request parameters passed through GET request from Client to Server ?

Anonymous said...

URL Encoding Example: converting “These’re #1!” into “These%27re+%231%21″
URL Rewriting Example: Converting gt; into gt;

Post a Comment