• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

encodeRedirectURL() and encodeURL()

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the differences between these two methods inside the HttpServletResponse interface?
public String encodeRedirectURL(String url)

public String encodeURL(String url)
Both of these methods support adding the SessionID into the URL if necessary. When do you use particular one for which purpose?

Thanks for your help.
 
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,
When you use URL rewritting for session tracking and you send a URL which references your site to the client, you will use these 2 methods.
The difference between these 2 is:
1) You use "public String encodeURL(String url)" to the situation when the URLs are embedded in the web page that the servlet generates, eg.
String oldURL = "/somepackage/someservlet1";
String newURL = response.encodedURL(oldURL);
out.println("<A HREF=\"" + newURL + "\">go here</A>");
2) You use public String encodeRedirectURL(String url) to the situation that you use a URL that refers to resource which you will use sendRedirect call. eg.
String oldURL = "/somepackage/someservlet2";
String newURL = response.encodedRedirectURL(oldURL);
response.sendRedirect(newURL);

Hope this helps you understand these 2 methods.
 
Joe Man
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answer.
So you saying that encodeRedirectURL(String url) should always go together with sendRedirect(newURL)? I can't use for example:
String oldURL="/String oldURL = "/somepackage/someservlet2";
String newURL = response.encodeURL(oldURL);
response.sendRedirect(newURL);
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it! Because All URLs sent to the HttpServletResponse.sendRedirect method should be run through the method public java.lang.String encodeRedirectURL(java.lang.String url).
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I've done exactly that (used response.sendRedirect(response.encodeURL(url)) and it has worked fine. Does anyone know exactly what the differences are?
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, u should try this after disabling cookies and in second page try to access third through link(no form)
 
reply
    Bookmark Topic Watch Topic
  • New Topic