Multiple choice technology architecture

Given req is an HttpServletRequest, which snippet creates a session if one doesn't exist?

  1. req.getSession(false);

  2. req.getSession(true);

  3. both the above

  4. none of the above

Reveal answer Fill a bubble to check yourself
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.