File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Sessions Doubt?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Sessions Doubt??" Watch "Sessions Doubt??" New topic

Sessions Doubt??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10033

Hi Guys,

I was trying out some examples on sessions. Here goes my code as below,

My welcome file,

<form action="" method="GET">
<input type = "text" name="Username">
<input type="submit" value="Submit">

On hitting submit, it takes me to where I have the following code in my doGet method...

PrintWriter out = response.getWriter();
out.println("Testing sessions for " + request.getParameter("Username"));
HttpSession session = request.getSession();
out.println("This is a new session");
out.println("Welcome back");

When I ran it, it always gives me Welcome back..??? Even for the first request, it gives me Welcome back...How is this possible?? I'm confused! Please anyone help me on this.

Thanks in advance!

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Swetha S

Joined: Dec 06, 2006
Posts: 16
Hi Jyothi,

You are using
HttpSession session = request.getSession();
which always returns an already existing session or creates a new session and gives you.Hence session.isNew() is always false.

if you use like this
HttpSession session = request.getSession(false);
Roger Stratus

Joined: Jan 07, 2007
Posts: 3
Please read the following Servlet Documentation

public boolean isNew()Returns true if the Web server has created a session but the client has not yet joined. For example, if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new.

true if the server has created a session, but the client has not yet joined

IllegalStateException - if the session is invalid
Elan Ram
Ranch Hand

Joined: Dec 14, 2006
Posts: 40
Hi Jothi,

what is your welcome file extension? Is it html or jsp?

Thanks and regards,
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10033

My welcome file is a html?? But does that really matters??
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
The browser might have disabled cookie. you can check whether jsessionid is really present in the cookie header of the response to the client.

Hard work rewards
I agree. Here's the link:
subject: Sessions Doubt??
It's not a secret anymore!