JBridge

JBridge: Certification Question Of The Day

JBridge Home >> Certification Questions >> Today's Question Sunday 4th 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 {
	long l = request.getDateHeader("Date");
	Date d = new Date(l);
	System.out.println(d);
}



A “null” is output to the console.
B A date is output to the console.
C A NumberFormatException is thrown.
D A compilation error occurs.
E A DateHeaderException is thrown.
F An IllegalArgumentException is thrown.
Page down for the answer...











































The Answer

The correct answer is B – a date is output to the console. When getDateHeader() receives a header name that isn’t in the request, it returns a -1 long value. -1 is a valid date as far as the Date constructor is concerned. On my system, the date printed out is 1 second before one o’clock on Thursday 1st January 1970 (Greenwich Mean Time).
So “null” is not printed – answer A is wrong.
There’s no such thing as a DateHeaderException (unless you have written one) – so E is wrong.
There is such a thing as a NumberFormatException. This can be thrown from one of HttpServletRequest’s companion methods, request.getIntHeader(). But not from getDateHeader(); so C is wrong.
There is also an IllegalArgumentException, and what is more getDateHeader() can throw this exception. However, this would only have happened in the above example if the header “Date” passed from the browser had contained a value, BUT that value, when parsed, could not be made to yield a valid date. So the circumstances are wrong for F.
There’s nothing syntactically wrong with the code, so D is also 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.