Thursday, January 19, 2017

Difference between URL-rewriting URL-encoding in Servlet JSP

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

Btw, if you are new into Servlet and JSP then I also suggest you go through a comprehensive course to learn Servlet and JSP in depth. If you need a recommendations then I suggest you check out JSP, Servlet, and JDBC for Beginners: Build a Database App course on Udemy by Chad Darby. One of the hands-on course to learn Servlet and JSP. 

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 the server and need to rewrite the URL with 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 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
Spring Framework 5: Beginner to Guru
JSP, Servlets and JDBC for Beginners: Build a Database App

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