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

encode(Redirect)URL, explain it please!

 
Jonny Andersson
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just realized that I don�t yet fully understand the differece between the two methods encodeURL and encodeRedirectURL for response. After reading at page 237 in Head First Servlets & JSP (that I guess many other here is reading) did I think that encodeRedirectURL should encode my URL (in case of need) AND immediately redirect to the given URL. But now I understand that it only encodes the URL and that I still have to do the response.sendRedirect.

But why can I then not just use the simple encodeURL method instead? Why do I have to use encodeRedirectURL? I understand that I have to use encodeRedirectURL after reading the javadoc documentation for the API but I don�t understand why? I dont�t understand what logic that they talk about in the javadoc docmentation tha encodeRedirectURL gives me and that require it to be used for response.sendRedirect.

Can anyone explain to me?

And remember that the book everywhere says "there are no dumb questions"! : )
 
S Subramonyan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

encodeRedirectUrl(string url) is used when you don't have cookies supported in the destination browser. It adds the jsessionid as part of the url. Its called Url Re-writing.

When using sendRedirect using the normal url in browsers that don't support cookies, the browser client will not participate in the current session. To enable this, you have to explicitly make the client a part of the current session.
Eg:

String url = http://mywebsite/accountinfo

sendRedirect(url) -----------> not Wrong, but will not give expected results.

sendRedirect(encodeRedirectUrl(url)) ---------------> Will give expected results.

Hope this helps.
 
Jonny Andersson
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer! But I already understand that I have to encode the URL to have the browser clients that not support cookies to participate in a session. My question is therefore instead about why I have to use encodeRedirectURL in a call to sendRedirect instead of encodeURL? Why do I have to write response.sendRedirect(encodeRedirectURL("website.com/goodies/")) instead of response.sendRedirect(encodeURL("website.com/goodies/"))?

The most close to an answer to this I have seen until now is the answer at the bottom of this tread and could that answer be true? Is it true that encodeURL does something more than encodeRedirectURL that cause the need to use encodeRedirectURL for sendRedirect calls instead of encodeURL?

I am lucky that this knowledge is probably not something that is required to know for the exam : ) ... It�s more that I am curious.

Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic