How would servlet code from a service method (e.g.,
doPost()
) retrieve the value of the “User-Agent” header from the request? (Choose all that apply.)( API)
A.
String userAgent = request.getParameter("User-Agent");
B.
String userAgent = request.getHeader("User-Agent");
C.
String userAgent = request.getRequestHeader("Mozilla");
D.
String userAgent = getServletContext().getInitParameter("User-Agent");
Which HTTP methods are used to show the client what the server is receiving? (Choose all that apply.)
(HF 4, HTTP methods)
Which method of
HttpServletResponse
is used to redirect an HTTP request to another URL?(API)
Which HTTP methods are NOT considered idempotent? (Choose all that apply.)
(HF 4, idempotent requests)
Given
req
is aHttpServletRequest
, which gets a binary input stream? (Choose all that apply.)(API)
A.
BinaryInputStream s = req.getInputStream();
B.
ServletInputStream s = req.getInputStream();
C.
BinaryInputStream s = req.getBinaryStream();
D.
ServletInputStream s = req.getBinaryStream();
How would you set a header named “CONTENT-LENGTH” in the
HttpServletResponse
object? (Choose all that apply.)(API)
A.
response.setHeader(CONTENT-LENGTH,"1024");
B.
response.setHeader("CONTENT-LENGTH","1024");
C.
response.setStatus(1024);
D.
response.setHeader("CONTENT-LENGTH",1024);
Choose the servlet code fragment that gets a binary stream for writing an image or other binary type to the
HttpServletResponse
.(API)
A.
java.io.PrintWriter out = response.getWriter();
B.
ServletOutputStream out = response.getOutputStream();
C.
java.io.PrintWriter out = new PrintWriter(response.getWriter());
D.
ServletOutputStream out = response.getBinaryStream();
Which methods are used by a servlet to handle form data from a client? (Choose all that apply.)
(API)
A.
HttpServlet.doHead()
B.
HttpServlet.doPost()
C.
HttpServlet.doForm()
D.
ServletRequest.doGet()
E.
ServletRequest.doPost()
F.
ServletRequest.doForm()
Which of the following methods are declared in
HttpServletRequest
as opposed to inServletRequest
? (Choose all that apply.)(API)
How should servlet developers handle the
HttpServlet
’sservice()
method when extendingHttpServlet
? (Choose all that apply.)(API)
A.
They should override the
service()
method in most cases.B.
They should call the
service()
method fromdoGet()
ordoPost()
C.
They should call the
service()
method from theinit()
method.D.
They should override at least one
doXXX()
method (such asdoPost()
).
Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.