Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between encodeURL() and encodeRedirectURL() methods

 
Mangesh Tendulkar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Srikanth Nutigattu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic