JBridge

JBridge: Certification Question Of The Day

JBridge Home >> Certification Questions >> Question for Friday 16th May 2003 Friday 16th May 2003

Given the following servlet code, identify what will happen when a HEAD request is made to the servlet's URL. (1 correct answer)

11 public class TestHeadRequest extends HttpServlet {	
12   protected void doGet(HttpServletRequest request, HttpServletResponse  
13                       response) throws ServletException, IOException {
14     System.out.println("In TestHeadRequest");
15     PrintWriter out = response.getWriter();
16     out.write("Method Executed");
17   }
18 }



A "In TestHeadRequest" is output to the console.
B "In TestHeadRequest" is output to the console and "Method Executed" is returned in the response body.
C Nothing is output to the console or returned to the response body.
D An HTTP response code 405 (Method Not Allowed) is returned to the requester.
E Line 16 does not execute.
Page down for the answer...











































The Answer

The correct answer is A, "In TestHeadRequest" is returned to the console. When the doHead() method from the parent servlet is not overridden, and the servlet receives a HEAD request, the doGet() method is executed.
No response body is returned when a HEAD request is made, hence answer B is wrong.
Since the doGet() method is executed, there is output to the console, so ruling out answer C.
A response code of 405 sounds plausible: after all, there is no overriding doHead() method to match the HEAD request. In fact, you DO get a 405 error if the servlet only contains a doPost() method and nothing else. Under these circumstances, the doHead() of the parent servlet attempts to forward to the doGet() method - and there isn't one. Anyway, for the code as written, no 405 error results so answer D is wrong.
And although no response body is returned, the code generating the response body (including line 16) still executes. So answer E is wrong.

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.