JBridge

JBridge: Certification Question Of The Day

JBridge Home >> Certification Questions >> Question for Wednesday May 14th 2003 Wednesday May 14th 2003

Identify true statements about the following servlet code (the line numbers are not part of the code) (2 correct answers)

11  RequestDispatcher rd = request.getRequestDispatcher("/InventoryServlet");
12  PrintWriter out = response.getWriter();
13  out.write("Inventory Report");
14  rd.forward(request, response);
15  out.flush();
16  out.close();



A The servlet will not run, as it will not compile.
B The output from "InventoryServlet" is returned to the requester.
C The output from "InventoryServlet" is returned to the requester, prefaced by the words "Inventory Report".
D A runtime error occurs at line 14.
E Lines 15 and 16 do not execute.
F Lines 15 and 16 do execute.
G Lines 15 and 16 do not necessarily execute (it depends on the servlet container implementation)
H In line 11, the "/" in "/InventoryServlet" is incorrect. Only a RequestDispatcher returned from ServletContext can have a getRequestDispatcher parameter beginning with a "/".
Page down for the answer...











































The Answer

B and F are the correct answers.
The output from InventoryServlet is the only thing returned to the requester; the output from the current servlet is abandoned (that is the nature of a forward). However, lines 15 and 16 do execute, and this is not container-dependent. (So answers E and G are wrong).
The servlet compiles just fine, invalidating answer A.
The words "Inventory Report" from this servlet are ignored after the forward takes place, and so not returned to the requester; answer C is wrong.
Answer D is wrong: line 14 does not give rise a runtime error - this would only occur if the response from the current servlet had been committed (flushed) prior to the forward being invoked.
Finally, answer H is meant to make you think this is the root cause of the compilation error (or perhaps gives rise to a runtime error). The opposite of the truth is stated here. A RequestDispatcher returned from ServletContext does require "/" at the beginning of parameters to getRequestDispatcher. However, the opposite of this is not true: a RequestDispatcher returned from the Request can legitimately have "/" (or not) at the beginnng of parameters to getRequestDispatcher.


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.