• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

difference between encodeRedirectURL and encodeURL

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm... Thanks Mark.
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic