• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

encodeRedirectURL() and encodeURL()

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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: 5390
1
Spring Java
  • Mark post as helpful
  • send pies
  • 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)
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic