File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes URL rewriting? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "URL rewriting?" Watch "URL rewriting?" New topic
Author

URL rewriting?

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


I hope the above code is right example of keeping track of sessions if cookies are enabled at client...

I am not clear with URL rewriting, how we use it?

Could any body please give little code, how to use it??

Thanks a lot.
[ December 05, 2005: Message edited by: rathi ji ]
Albin Joseph
Greenhorn

Joined: Jan 22, 2005
Posts: 21
HttpSession by default attempts to store the session in Cookies. So if cookies are disabled you need to use URL rewritting. For this you have two methods in HttpServletResponse. encodeURL and encodeRedirectURL. It will append the jsessionid paramater to your URL. If no encoding is needed it will return the URL unchanged.

you can use it like respons.sendRedirect(response.encodeRedirectURL("myrurl"));


Java source code examples | Java coding community.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Albin Joseph:
HttpSession by default attempts to store the session in Cookies. So if cookies are disabled you need to use URL rewritting. For this you have two methods in HttpServletResponse. encodeURL and encodeRedirectURL. It will append the jsessionid paramater to your URL. If no encoding is needed it will return the URL unchanged.

you can use it like respons.sendRedirect(response.encodeRedirectURL("myrurl"));


Yes, but when to use this code, where??

Thanks.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


Yes, but when to use this code, where??


Anywhere your servlet/jsp generates a link in the response html and you are not sure about the browser cookie handling capabilities.

ram.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by ramprasad madathil:


Anywhere your servlet/jsp generates a link in the response html


But when we generate link in response??, we either write HTML in response or forward request and response to other servlet...

Please comments.
Thanks.


[ December 05, 2005: Message edited by: rathi ji ]
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

>we either write HTML

You always write html in response and that html may contain href elements. See below.




Now the link above, should/can be rewritten in the jsp







ram.

[ December 06, 2005: Message edited by: ramprasad madathil ]
[ December 06, 2005: Message edited by: ramprasad madathil ]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Thanks Ram,

It is clear but suppose there is no need to write URL in response then how do we call response.encodeURL() method or how do we tell the container that use URL rewriting if cookies fail...

Thanks.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

suppose there is no need to write URL in response then how do we call response.encodeURL() method




Thats where the trick is. Unlike the getSession() method which does all the work of sending the sessionID to browser and receiving the sessionID from the request, In URL Rewriting it's your responsibility to send the sessionID to user. How to do this?

In URL Rewriting the sessionID will not come to your servlet code automatically like sessions. There has to be a link that contains the sessionID stuffed at the end of the URL as explained by the code given by ram. So that when the user clicks the link the sessionID comes as part of the request and the server can identify the sessionID and continue the session.

Note: SessionID are automatically generated and stuffed at the end of the URL by the container


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Rahim Vindhani
Greenhorn

Joined: Dec 06, 2005
Posts: 10
Originally posted by Albin Joseph:
HttpSession by default attempts to store the session in Cookies. So if cookies are disabled you need to use URL rewritting. For this you have two methods in HttpServletResponse. encodeURL and encodeRedirectURL. It will append the jsessionid paramater to your URL. If no encoding is needed it will return the URL unchanged.

you can use it like respons.sendRedirect(response.encodeRedirectURL("myrurl"));


Exactly... but again you need to encode each and every URL of your page. Otherwise this technique wont work.


Rahim Vindhani,<br /><a href="http://www.java-examples.com" target="_blank" rel="nofollow">Java Examples</a>
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

Rahim has it right.
You have to understand that rewriting your url doesnt ensure that the session id is appended to the url.

Here's the javadoc for encodeURL() method

--------------------------
Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. For example, if the browser supports cookies, or session tracking is turned off, URL encoding is unnecessary.
---------------------------

What this means is that even if you rewrite your url, the container would be 'smart' enough to use it on a 'in need' basis - if the container detects that the browser doesnt support cookies, it appends the session id to the url provided you have rewritten it . If the browser does accept cookies, the session id wouldnt be used even if you have passed your url string through the encodeURL() method.

Ofcourse, since you have no prior knowledge of the client browser settings, especially in internet applications, its considered good practise to encode all your urls.

This is exactly what the doc says too - see below

---------------------------------
For robust session tracking, all URLs emitted by a servlet should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies.
---------------------------------

cheers,
ram.
JuanP barbancho
Ranch Hand

Joined: Oct 25, 2005
Posts: 52
Hi,

I use cookies and URL rewritte when I cannot put cookies.

I use an session attribute for the action of the post. I put this session attributte in the JSP.

I calculate the session attributte when the user log and put in session.

some code
 
GeeCON Prague 2014
 
subject: URL rewriting?