This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Difference between encodeURL() and encodeRedirectURL() methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Difference between encodeURL() and encodeRedirectURL() methods" Watch "Difference between encodeURL() and encodeRedirectURL() methods" New topic
Author

Difference between encodeURL() and encodeRedirectURL() methods

Mangesh Tendulkar
Greenhorn

Joined: Mar 20, 2010
Posts: 8
Hi All,

I am trying some snippets for preparing myself for SCWCD. While studying encodeURL() and encodeRedirectURL(), I found that using any of these does not make any difference if you are going to redirect the request to say some other jsp page or servlet. Below is the code snippet i tried:

//String encUrl = res.encodeURL("/scwcd/showServletContextInformation");
//String encUrl = res.encodeRedirectURL("/scwcd/showServletContextInformation");
System.out.println("****Encoded url is: "+encUrl+" ****");
res.sendRedirect(encUrl);

here /scwcd is context root. Is my understanding right or am I missing on something important!

Regards,
Mangesh


SCJP 6.0 (100%)
SCWCD 5.0 (100%)
SCBCD 5.0 (98%)
Srikanth Nutigattu
Ranch Hand

Joined: Oct 30, 2007
Posts: 114
According to Servlet API,

http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletResponse.html#encodeRedirectURL(java.lang.String)

The main difference is:
If session id is to maintained using URL (if you dont want/cant use cookies), or want to encode normal url's where you don't bother about Sessions.


Mangesh Tendulkar
Greenhorn

Joined: Mar 20, 2010
Posts: 8
Yes perfectly said however i was trying to figure out situations in which one has to use specific method. I am reading SCWCD by Charles Lyons, it mentions that for sendRedirect() one has to use encodeRedirectURL() whereas when one wants to use hyperlinks on page then use encodeURL(). Also it mentions that sometimes (not always) the output of both these methods differ, so I wanted to understand when that happens. Thats why out of curiosity, I tried to use encodeURL() in combination with sendRedirect() and it works just same as with encodeRedirectURL(). I am definitely missing on something, but dont know what

Regards,
Mangesh
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
The API doesn't tell us what these methods do differently. Only that you are supposed to use the one when using redirect, and the other in other cases.
What's going on behind the scenes we're not supposed to care about.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between encodeURL() and encodeRedirectURL() methods
 
Similar Threads
difference between encodeRedirectURL and encodeURL
Difference between encodeURL and encodeRedirectURL
Hints for SCWCD
encodeURL and encodeRedirectURL
listener question