Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

encodeRedirectURL() and encodeURL()

 
Joe Man
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.
 
Tong Chen
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).
 
Paul Jonestone
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?
 
R K Singh
Ranch Hand
Posts: 5384
  • 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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic