JBridge

JBridge: Certification Question Of The Day

JBridge Home >> Certification Questions >> Today's Question Tuesday 6th May 2003

What will be the outcome of placing the following code in a servlet’s doGet method?

String s = request.getHeaders(“Accept”);
PrintWriter out = response.getWriter();
out.write(s);



A When a browser makes a request of the servlet, output like the following is seen in a single line on the resulting web page:
image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
B The code will not compile.
C When a browser makes a request of the servlet, output like the following is see on separate lines in the resulting web page:
image/gif
image/x-xbitmap
image/jpeg
… more similar lines …
*/* 

D An IOException is thrown on accessing the servlet.
E A browser error occurs because the resulting web page is not in proper HTML format.
F An IllegalArgumentName is thrown because “Accept” is not a valid HTML header field.
Page down for the answer...











































The Answer

The answer is B – the code will not compile. The reason is that request.getHeaders(String name) returns an Enumeration; it’s the method request.getHeader(String name) that returns a String.
Had the method been request.getHeader(), the output in answer A would have been plausible.
The output in C would only happen if the client had sent each allowed file type in an “Accept” header on a separate line, and there had been code in the servlet to print out each of these.
The IOException in D is a complete red herring.
The browser error in E sounds plausible, but there is no compunction for a servlet to return valid HTML (a polite servlet might have set the content type, though).
The IllegalArgumentName in F again sounds plausible, but even if the “Accept” had not been a valid header (it is) or had not been present, the worst that “request.getHeaders()” would do is to return an empty enumeration.


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.