File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes encodeRedirectURL() and encodeURL() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "encodeRedirectURL() and encodeURL()" Watch "encodeRedirectURL() and encodeURL()" New topic

encodeRedirectURL() and encodeURL()

Joe Man
Ranch Hand

Joined: Feb 17, 2002
Posts: 71
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.

Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform
Tong Chen
Ranch Hand

Joined: Apr 26, 2002
Posts: 1011
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);

Hope this helps you understand these 2 methods.

Joe Man
Ranch Hand

Joined: Feb 17, 2002
Posts: 71
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);
Tong Chen
Ranch Hand

Joined: Apr 26, 2002
Posts: 1011
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

Joined: Jun 24, 2002
Posts: 2
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

Joined: Oct 15, 2001
Posts: 5382
I think, u should try this after disabling cookies and in second page try to access third through link(no form)

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
I agree. Here's the link:
subject: encodeRedirectURL() and encodeURL()
It's not a secret anymore!