Coffee Cram: Chapter 4 Answers

  1. 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");

    Note

    -Option B shows the correct method call passing in the header name as a String parameter.

  2. Which HTTP methods are used to show the client what the server is receiving? (Choose all that apply.)

    (HF 4, HTTP methods)

    A.

    GET

    B.

    PUT

    C.

    TRACE

    D.

    RETURN

    E.

    OPTIONS

    Note

    -This method is typically used for troubleshooting, not for production.

  3. Which method of HttpServletResponse is used to redirect an HTTP request to another URL?

    (API)

    A.

    sendURL()

    B.

    redirectURL()

    C.

    redirectHttp()

    D.

    sendRedirect()

    E.

    getRequestDispatcher()

    Note

    - Option D is correct, and of the methods listed, it’s the only one that exists in HttpServletResponse

  4. Which HTTP methods are NOT considered idempotent? (Choose all that apply.)

    (HF 4, idempotent requests)

    A.

    GET

    B.

    POST

    C.

    HEAD

    D.

    PUT

    Note

    -By design, POST is meant to convey requests to update the state of the server. In general the same update should not be applied multiple times.

  5. Given req is a HttpServletRequest, 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();

    Note

    -Option B specifies the correct method and the correct return type.

  6. 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);

    Note

    -Option B shows the correct way to set an HTTP header with two String parameters, one representing the header name and the other the value.

  7. 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();

    Note

    -Option A is incorrect because it uses a character-oriented PrintWriter

  8. 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()

    Note

    -Options C-F are wrong because these methods don’t exist.

  9. Which of the following methods are declared in HttpServletRequest as opposed to in ServletRequest? (Choose all that apply.)

    (API)

    A.

    getMethod()

    B.

    getHeader()

    C.

    getCookies()

    D.

    getInputStream()

    E.

    getParameterNames()

    Note

    -Options A, B, and C all relate to components of an HTTP request.

  10. How should servlet developers handle the HttpServlet’s service() method when extending HttpServlet? (Choose all that apply.)

    (API)

    A.

    They should override the service() method in most cases.

    B.

    They should call the service() method from doGet() or doPost()

    C.

    They should call the service() method from the init() method.

    D.

    They should override at least one doXXX() method (such as doPost()).

    Note

    -Option D is correct, developers typically focus on the doGet(), and doPost() methods

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.