[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Web Component Certification (SCWCD)
 
RSS feed
 
New topic
Author

Session Management

Steve Jerome
Greenhorn

Joined: Mar 03, 2007
Messages: 22

public void doGet(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException
{
HttpSession ses=req.getSession();
PrintWriter out=res.getWriter();
if(ses.isNew())
out.println("new session");
else
out.println("welcome back");
}
}

In the above example if the cookies are disabled then the program will always written new session.
But I've tried this program after disabling the cookies but it writtens welcome back when I refresh the window.How is it possible?
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Messages: 280

If cookies are disabled then container will automatically fall back for URL rewriting for session management.

Hope it helps

"Know where to find the solution and how to use it - that's the secret of success."
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Messages: 372

Yes, the container will fall back to URL Writing, but this is only if we write the code for it..

SCJP 1.4, SCWCD 1.4, SCBCD 1.5
Steve Jerome
Greenhorn

Joined: Mar 03, 2007
Messages: 22

But I didn't write any code for URL rewriting.Can anyone try this program and tell what happens.
Shrenik Sakriya
Ranch Hand

Joined: May 07, 2001
Messages: 40

As far as i know the server first tries to use Cookie by default as a session tracking mechanism, if the cookies are disabled then it uses URL Rewriting by default. Hence in your case the session is still active.

Shrenik Sakriya

SCJP1.2 88%<br />SCWCD1.4 89%<br />CCNA 94%
Steve Jerome
Greenhorn

Joined: Mar 03, 2007
Messages: 22

yeah that's right only if you use URL rewriting in your code.but I didn't use URL rewritig as I said before.
Johan Pelgrim
Ranch Hand

Joined: Jul 07, 2003
Messages: 60

Your code works fine in my case, i.e. it only shows "new session" when refreshing the page and cookies are disabled (I'm using Firefox). Are you sure cookies are disabled in your browser? (just checking)
[ March 23, 2007: Message edited by: Johan Pelgrim ]

Johan Pelgrim, The Netherlands
SCJP 1.4, SCWCD 1.4, [Studying for SCBCD 5 & Spring Framework Certifications]
warren li
Ranch Hand

Joined: May 23, 2006
Messages: 128

Originally posted by Shrenik Sakriya:
As far as i know the server first tries to use Cookie by default as a session tracking mechanism, if the cookies are disabled then it uses URL Rewriting by default. Hence in your case the session is still active.

Shrenik Sakriya


No. the server will use both at the first response if you did use URL rewriting in your code. if cookie is enabled, then subsequent responses will ignore URL rewriting in your code. otherwise, they will use URL rewriting.

Think about it. If hte server only uses cookie as default on the first response, what if the cookie is disabled by the user? then the next request comes in without any session id information, then the server will have to create a new session. This is certainly not what we want.

SCBCD 1.3: 94%<br />SCWCD 1.4: 91%<br />SCJP 5: 95%
Steve Jerome
Greenhorn

Joined: Mar 03, 2007
Messages: 22

I tried firefox and the program works fine dispalying new session everytime.but it doesn't work with IE.can anyone tell me what's the problem.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Messages: 329

Originally posted by Steve Jerome:
but it doesn't work with IE.can anyone tell me what's the problem.


IE?

Try flushing your cookies and then doing a refresh.

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Steve Jerome
Greenhorn

Joined: Mar 03, 2007
Messages: 22

Originally posted by Sergio Tridente:


IE?

Try flushing your cookies and then doing a refresh.


IE-Internet Explorer.I've already tried deleting the cookies but no use.can anyone able to run the program in internet explorer?
Sunder Ganapathy
Ranch Hand

Joined: Apr 01, 2003
Messages: 120

I ran the program on IE 6. No problem faced. I kept 'Medium High' in the Privacy tab of Tools/Internet Options.

First time you get new session and when you refresh you get welcome back.
Steve Jerome
Greenhorn

Joined: Mar 03, 2007
Messages: 22

Originally posted by Sunder Ganapathy:
First time you get new session and when you refresh you get welcome back.


Actually the program runs fine and the output is what you are getting with cookies enabled.But the problem is running the program after disabling cookies(Read the first Question of the forum).When you disable cookies you should get only new session every time you refersh the browser.But instead welcome back is printing.
Clifton Eaton
Greenhorn

Joined: Mar 26, 2007
Messages: 16

Apparently the difficulty you are having in IE is due to the way IE treats the localhost domain. I was able to repeat your problem and after some research uncovered this article:

http://www.techfeed.net/blog/index.cfm/2005/11/7/IE-cookies-and-localhost

It identifies the problem and then a comment on the blog gives the solution for testing... and not it's not always use firefox

-------------------------------------<br />SCJP<br />SCWCD
Steve Jerome
Greenhorn

Joined: Mar 03, 2007
Messages: 22

Thanks Eaton for your solution.now my code works fine.
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Web Component Certification (SCWCD)
 
RSS feed
 
New topic
replay challenge

.