Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A QUESTION ABOUT SESSION

 
Lei Xiaowen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given an HttpServletRequest request and an HttpServletResponse response:
41. HttpSession session = null;
42. // insert code here
43. if(session == null) {
44. // do something if session does not exist
45. } else {
46. // do something if session exists
47. }
To implement the design intent, which statement must be inserted at line 42?
A. session = response.getSession();
B. session = request.getSession();
C. session = request.getSession(true);
D. session = request.getSession(false);
E. session = request.getSession("jsessionid");

the answer is D.why answer B is not correct??? and A?
 
Pawan Ramchandani
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

getSession()
Returns the current session associated with this request, or if the request does not have a session, creates one.So if you are putting this method there then if block will never execute.

getSession(boolean)
Returns the current HttpSession associated with this request or, 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.

So that D is correct answer and you get session from request object not from response so option A is wrong choice.
[ August 11, 2005: Message edited by: Pawan Ramchandani ]
 
Aruneesh Salhotra
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to elaborate (by the way it is an interesting question). All the choices you mentioned seemed correct, but the key thing was the "design intent".
If you choice the other two, it will always go in the check where the session is always created.
 
Aruneesh Salhotra
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to elaborate (by the way it is an interesting question). All the choices you mentioned seemed correct, but the key thing was the "design intent".
If you choice the other two, it will always go in the check where the session is always created.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic