Tuesday, July 13, 2021

Difference between URL-rewriting URL-encoding in Servlet JSP? Answer

URL-rewriting vs URL-encoding in Servlet JSP
The 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 the client browser or browser doesn't support cookie while URL-encoding is a way to pass a string to the 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 sound 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 question, I have also shared some more questions on my posts 10 Servlet Interview questions answers and 10 JSP interview questions and answers for Java programmers.



Difference between URL Rewriting and URL Encoding in JSP Servlet

difference between URL-Rewriting and URL-Encoding in Java ServletYou often need to encode the URL before sending it to the server and need to rewrite the URL with the session id in order to maintain a session where the 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 the method itself and it doesn't embed sessionID if the browser supports cookies or session maintenance is not required. In order to implement 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 the JSTL core tag all URL passed to it will automatically be URL-rewriting if the browser doesn't support cookies.

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

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 the 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.


Some more Java posts you may like


2 comments :

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