wood burning stoves 2.0*
The moose likes Servlets and the fly likes difference between encodeRedirectURL and encodeURL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "difference between encodeRedirectURL and encodeURL" Watch "difference between encodeRedirectURL and encodeURL" New topic
Author

difference between encodeRedirectURL and encodeURL

Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
what is the difference between encodeRedirectURL and encodeURL methods.
I looked in the API but could not understand the difference between these two methods.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
The javadoc for encodeRedirectURL says:
Because the rules for making this determination can differ from those used to decide whether to encode a normal link, this method is separated from the encodeURL method.

They're telling you that it's different, but you don't need to know why it's different
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Because the rules for making this determination can differ from those used to decide whether to encode a normal link, this method is separated from the encodeURL method.


What did they mean by saying a normal link,
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
In your servlet code, you can encode a URL for simple display in the page, or you can encode a URL for use by the sendRedirect method. When you which to pass a URL to sendRedirect, you need to use the encodeRedirectURL method.
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Mark E Hansen wrote: When you which to pass a URL to sendRedirect, you need to use the encodeRedirectURL method.


As i understand from this, you are trying to say that , unless encodeRedirectURL is used, urls will not be encoded for sendRedirect ??

but i have used and it works fine

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
The API is telling you not to do that. Do you really want to write code that violates the API documentation? Especially when it specifically tells you not to do that?

Again, we don't need to know why they have a separate method for the redirect case. They do, and we should use it.

I realize you just want to know why, but they don't want to tell us (officially, anyway). I guess you could hunt down the source code and have a look at the method implementations - that would tell you why they are different.
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Ok .. so can you give me an example for each method about when to use?

You have already said that when sendRedirect is needed we should use encodeRedirectURl.
for encodeURL --???
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
Sony Agrawal wrote:Ok .. so can you give me an example for each method about when to use?

You have already said that when sendRedirect is needed we should use encodeRedirectURl.
for encodeURL --???


Any other case. How about a forward, as in:
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Hmm... Thanks Mark.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

Any other case. How about a forward



Just curious - why should the encodeURL() be called on the response before an internal forward? The incoming request either has a session id (through a cookie/url rewriting) or it does not. If it does not have, calling encodeURL() before an internal forward would not make any difference, right?

ram.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Mark E Hansen wrote:
Any other case. How about a forward, as in:


The encodeURL method embeds the session ID into the URL so it would be pointless to use it for a server side forward.
The encodeURL method is useful if you are building up a hyperlink href attribute or an image src attribute to be written directly to the browser.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
itrahul soni
Greenhorn

Joined: Feb 01, 2010
Posts: 6
encodeURL is used with RequestDispatcher when you want to keep track of session so the jsessionid is added to your url.while if you want to use sendRedirect with jsessionid at that time you will use encodeRedirectURL method which is alternative of sendRedirect with jsessionid.

Got the Point!
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

encodeURL is used with RequestDispatcher when you want to keep track of session so the jsessionid is added to your url.


A RequestDispatcher is used to either forward the request to a resource or include the contents of another resource. Irrespective of that, it is a server side paradigm.

encodeURL() as has already been pointed out twice has not got nothing to do with RequestDispatchers. It is used in jsps and/or Servlets only when constructing a url (a link, a form post url etc).

ram.
itrahul soni
Greenhorn

Joined: Feb 01, 2010
Posts: 6
Yes you are right ramprasad madathil, encodeURL has nothing to do with RequestDispatcher.it only wraps your URL with jsessionid so when you right
response.encodeURL("/test.jsp"); it will encode the URL with jsessionid and response.encodeRedirectURL is for sendRedirect with jsessionid wrap or added with URL.

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
Ben Souther wrote:
Mark E Hansen wrote:
Any other case. How about a forward, as in:


The encodeURL method embeds the session ID into the URL so it would be pointless to use it for a server side forward.
The encodeURL method is useful if you are building up a hyperlink href attribute or an image src attribute to be written directly to the browser.


Thanks, I stand corrected.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between encodeRedirectURL and encodeURL
 
Similar Threads
response.encodeRedirectURL
encodeURL & encodeRedirectURL
encodeURL() v/s encodeRedirectURL()
encodeURL vs encodeRedirectURL
encodeURL and encodeRedirectURL