File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "confusion" Watch "confusion" New topic


Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I am confused about the differences between response.encodeURL() and URLEncoder.encode(). Please help.
Sean MacLean
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
Well, I think they simply have different purposes. Here's what the api's say
HttpServletResponse.encodeURL( String url )
"Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. For example, if the browser supports cookies, or session tracking is turned off, URL encoding is unnecessary.
For robust session tracking, all URLs emitted by a servlet should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies."
And the very old
The class contains a utility method for converting a String into a MIME format called "x-www-form-urlencoded" format.
To convert a String, each character is examined in turn:
"The ASCII characters 'a' through 'z', 'A' through 'Z', '0' through '9', and ".", "-", "*", "_" remain the same.
The space character ' ' is converted into a plus sign '+'.
All other characters are converted into the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the lower 8-bits of the character."
So basically, the servlet version determines if the session object id needs to be stored in the query string (because the client doesn't accept cookies, for instance). The version simply parse a query string into the various %20 and + signs etc. that we are used to seeing in the query string.
I agree. Here's the link:
subject: confusion
It's not a secret anymore!