JBridge

JBridge: Certification Question Of The Day

JBridge Home >> Certification Questions >> Today's Question Last updated: Friday 2nd May 2003

A browser sends a request containing no headers to a servlet. What is the result of executing the following method within the servlet?

		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String s = request.getHeader("Date");	
			System.out.println(s);
		}
		

A "null" is output to the resulting web page.
B A DateHeaderException is thrown.
C A date is printed on the returned web page.
D The code never runs, as the servlet will not compile.
E "null" is output to the console.

Page down for the answer...











































The Answer


The correct answer is E, "null" is output to the console. When the method getHeader(String headerName) receives a header name it doesn't recognize, it returns a null object. When System.out.println receives a null object reference as a parameter, it prints out the word "null".
Nothing is output to the web page; for that to happen, the method would need to retrieve a Writer or OutputStream from the response. The OutputStream associated with System.out.println is the console (for the application server running the servlet). So A and C could not be correct.
There is no such thing as a "DateHeaderException" - at least, not defined in javax.servlet or related packages - so B cannot be correct.
Finally, the code compiles just fine. The red herring factor here is that if you're retrieving a date, should the returned value not be long? So returning the value to a String would give you an incompatible type error? It would be if the method called was request.getDateHeader() - which it isn't. So that rules out answer D.

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.