JBridge

JBridge: Certification Question Of The Day

JBridge Home >> Certification Questions >> Question for Monday 2nd June 2003 Monday 2nd June 2003

What will be the outcome of a user accessing the following uncompiled JSP for the first time in a fresh browser session? (1 correct answer)
Note that the line numbers are for reference, as if the code appeared in a text editor.

10 <HTML><HEAD></HEAD><BODY>
11 <P>Session ID: <%-- <%= session.getId() %> --%></P>
12 <%@	page session = "false" %>
13 <% 
14   	HttpSession session = request.getSession(false);
15 	boolean b = (session == null) ? true : false; 
16 %>
17 <%= "There is no session: " + b %>
18 </BODY></HTML>


A A compilation error will occur because of the misplaced page directive at line 12, which should come before any JSP scriptlets or expressions.
B A compilation error will occur because the variable "session" will be declared twice - once as an implicit JSP variable, and again at line 14.
C A runtime error will occur, caused by a NullPointerException.
D Ouput will be obtained similar to the following:
Session ID:
There is no session: false
E Ouput will be obtained similar to the following:
Session ID:
There is no session: true
F Ouput will be obtained similar to the following:
Session ID: 00JPQXYABCD1234888894RTV
There is no session: false
Page down for the answer...











































The Answer

The correct answer is E - the code runs, and produces output as described.
The explanation is as follows. At line 11, the reference to the variable session is commented out. This is a good job. The only session variable that could exist at this point is the implicit one; and this is won't be declared thanks to the page directive at line 12.
The code at line 14 attempts to obtain a session from the request. However, it will only get one if a session already exists. There can't be a session from the JSP, because of the page directive. There could be a pre-existing session, except that the question is careful to specify that this is a FRESH browser session.
The session variable at line 14 will therefore be initialised to null, so causing the boolean variable at line 15 to evaluate as true.


EMail: dbridgewater@jbridge.co.uk
Phone: +44 (0)1943 877414
Fax: +44 (0)1943 877414
Mail: David Bridgewater, Willow Dene, Bradford Road, Menston, Ilkley, West Yorkshire, LS29 6ED, UK
Copyright © 2003 David Bridgewater. All rights reserved.