This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between encodeRedirectURL and encodeURL

 
Sony Agrawal
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 650
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 650
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 650
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 650
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... Thanks Mark.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
itrahul soni
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 650
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic