Hi, I am confused about the differences between response.encodeURL() and URLEncoder.encode(). Please help.
Joined: Nov 07, 2000
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 java.net.URLEncoder() 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 java.net version simply parse a query string into the various %20 and + signs etc. that we are used to seeing in the query string. Sean