When my servlet creates a session, even if it is the first time running the servlet, the session.isNew() returns false. I checked to make sure the session ID's are different each time I run it and they are, yet isNew() still returns false.

I'm trying to do the following in my servlet:

HttpSession session = request.getSession(true);
if (session.isNew())
{
                ...do something...
}

It never gets into that if statement because it always returns false :S

Any help would be appreciated.

First of all when you have questions such as this, always search the API.

http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpSession.html#isNew()

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html#getSession(boolean)

From the create session method, you see that it doesn't always create a new session. That call is the same as the no argument getSession() . The only difference is when you enter false. Then if there is no session it returns null.
But if you put true, it will not create a new session if one already exists.

I know that the above might no solve your problem because I just quoted the API, but have you tried running the session.invalidate() method first?

yeah I have tried running that first, however the problem has been solved. Thank you for posting.

Problem was solved how? This can help someone in the future...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.