Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Difference between encodeURL() and encodeRedirectURL() methods

 
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
 
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
 
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.
    Bookmark Topic Watch Topic
  • New Topic