Given req is an HttpServletRequest, which snippet creates a session if one doesn't exist?
-
req.getSession(false);
-
req.getSession(true);
-
both the above
-
none of the above
B
Correct answer
Explanation
HttpServletRequest.getSession(true) returns the current session or creates a new one if none exists. The boolean parameter 'create' controls this behavior: true means create if needed, false means return null if no session exists. Option A (getSession(false)) would return null instead of creating a session. The method getSession() with no arguments is equivalent to getSession(true) - it also creates sessions.