Saturday, July 12, 2014

URL encoding, decoding and the confusions you may have with Servlets

I had a lot of trouble with receiving HTTP URLs in my servlet (written in java) code.

Specifically when I do Ajax calls from Javascript (AngularJS) , the URLs were encoded and sent by the browsers with the space being replaced with %20 and probably some others which when I used the HttpServletRequest object with getRequestURL() to get the path segment of the HTTP URL ,

in my case it was like 'http://localhost:8080/abc/xyz/tags/Address of my home'. This URL is returned as 'http://localhost:8080/abc/xyz/tags/Address%20of%20my%20home'. This is something I thought was a trivial one and looked for answers in the world wide web. However after reading this post ,

http://blog.lunatech.com/2009/02/03/what-every-web-developer-must-know-about-url-encoding

which is written very well, I felt there is no point relying on the Servlet decode methods and waste my time. I decided to have my own little parser that essentially replaces the encoded characters as above to the spaces. I have some more in the URL query portion also.

Thought that if you are onto something like this, this may be of help.


No comments:

Post a Comment