The moose likes JSP and the fly likes cookies or url rewriting Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "cookies or url rewriting" Watch "cookies or url rewriting" New topic
Author

cookies or url rewriting

Kalpesh Soni
Ranch Hand

Joined: Jan 02, 2001
Posts: 306
I know its an age old question
but still i have to ask this one
The session api can use cookies or url-rewriting for maintaing sessions
Now what I need is a way not to use cookies, but only use url-rewriting
how can i make sure that session api does not use cookies
I see methods in javax.servlet.http. HttpServletRequest
like isRequestedSessionIdFromCookie()
but dont know how to set it


Test 094, IBM WID 6.0 cert
SCJP 1.2
SCBCD 1.3 Beta
SCWCD 1.4 Beta
SCMAD Beta
SCDJWS Beta

KS
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 259
if i remember right, it depends on the browser preferences. the servlet tries to set cookies but if they are disabled in browser it moves to session tracking stuff with urlencoding.
read more here:
http://csajsp-chapters.corewebprogramming.com/CSAJSP-Chapter9.pdf


Asher Tarnopolski
SCJP,SCWCD
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Originally posted by Asher Tarnopolski:
if i remember right, it depends on the browser preferences. the servlet tries to set cookies but if they are disabled in browser it moves to session tracking stuff with urlencoding.
read more here:
http://csajsp-chapters.corewebprogramming.com/CSAJSP-Chapter9.pdf

Sort of. URL rewriting must be done in the JSP in order to pass the session id. i.e. for a link to "myJsp.jsp" the session ID is not appended unless you use URLEncoder.encode("myJsp.jsp") to write the link. At that point, it is up to the servlet engine to determine how to encode it. In WAS, you either enable or diable URL encoding. With it enabled, the resulting link is something like "myJsp.jsp;ab234alkj2349023" which the browser understands is a session ID to pass back to the server.
If a browser has cookies 100% disabled and you don't use URL rewriting, the user will get a new session every time.

The Struts html:link tag takes care of all this for you, so if you need to maintain session across pages, it's a lot easier to use this tag
<html:link href="myJsp.jsp">link</a>
than
<a href="<%=URLEncoder.encode("myJsp.jsp")%>">link</a>
every time you create a link.


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 259
right, but you need response.encodeURL() method and not URLEncoder.encode() which encodes string into html format...
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Originally posted by Asher Tarnopolski:
right, but you need response.encodeURL() method and not URLEncoder.encode() which encodes string into html format...

DOH!
Of course, that's what I meant. :roll:
 
 
subject: cookies or url rewriting
 
Threads others viewed
Is cookies part of syllabus?
session management in servlets
enforcing URL rewriting in Tomcat
Confusion in URL Rewriting
How to use ckookie in jsp
developer file tools