aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes A QUESTION ABOUT SESSION Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "A QUESTION ABOUT SESSION" Watch "A QUESTION ABOUT SESSION" New topic
Author

A QUESTION ABOUT SESSION

Lei Xiaowen
Greenhorn

Joined: Jul 11, 2005
Posts: 15
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

Joined: Aug 15, 2003
Posts: 78
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 ]

Pawan Ramchandani<br />*******************************<br />SCJP 1.4<br />SCWCD <br />*******************************<br />Everything is okay in the end. If it's not okay, then it's not the end.
Aruneesh Salhotra
Ranch Hand

Joined: May 12, 2004
Posts: 167
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

Joined: May 12, 2004
Posts: 167
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A QUESTION ABOUT SESSION