getSession(false) is not null on first request

Hi everyone,

I got a weird problem with the session.

In my servlet, i have something like this:

if((req.getSession(false)) ==null)

{

dest="/WEB-INF/docs/authentication/login.jsp";

req.setAttribute("oriUrl",req.getRequestURL());

}

The getSession(false) always return something not null which is not what i expect. I though when this method is FIRST called on a new request, it will return null. Am i missing anything?

Thai

[686 byte] By [lnthai2002a] at [2007-11-27 7:50:52]
# 1

No, you're right; when called with false, if the session doesn't exist, it'll return null:

getSession

public HttpSession getSession(boolean create)Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.

If create is false and the request has no valid HttpSession, this method returns null.

To make sure the session is properly maintained, you must call this method before the response is committed.

Parameters:

true - to create a new session for this request if necessary; false to return null if there's no current session

Returns:

the HttpSession associated with this request or null if create is false and the request has no valid session

See Also:

getSession()

http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/http/HttpServletRequest.html#getSession(boolean)

Maybe you're doing something that causes creation of the session before?

nogoodatcodinga at 2007-7-12 19:31:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
The session is created in the first request for /. All request for / go to a frontController servlet. I scan through the frontController java file but i found nothing related to a session.
lnthai2002a at 2007-7-12 19:31:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
You probably have a JSP as your entrypoint? JSPs create sessions by default if needed.
jwentinga at 2007-7-12 19:31:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
@jwenting: Didn't you used to have a lot more Duke Stars? :-O
nogoodatcodinga at 2007-7-12 19:31:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

No, the request go to a front controller then the front controller forward the request to a jsp page. What i notice is that when the controller first receives the request, a session is created right away. In my web.xml. there is a session config section but it's just to set the max valid time for the session. I did tried to delete it but the session is still created .

lnthai2002a at 2007-7-12 19:31:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...