Coffee Cram: Mock Exam Chapter 4

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

    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");
  2. Which HTTP methods are used to show the client what the server is receiving? (Choose all that apply.)

    A.

    GET

    B.

    PUT

    C.

    TRACE

    D.

    RETURN

    E.

    OPTIONS

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

    A.

    sendURL()

    B.

    redirectURL()

    C.

    redirectHttp()

    D.

    sendRedirect()

    E.

    getRequestDispatcher()

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

    A.

    GET

    B.

    POST

    C.

    HEAD

    D.

    PUT

  5. Given req is a HttpServletRequest, which gets a binary input stream? (Choose all that apply.)

    A.

    BinaryInputStream s = req.getInputStream();

    B.

    ServletInputStream s = req.getInputStream();

    C.

    BinaryInputStream s = req.getBinaryStream();

    D.

    ServletInputStream s = req.getBinaryStream();

  6. How would you set a header named “CONTENT-LENGTH” in the HttpServletResponse object? (Choose all that apply.)

    A.

    response.setHeader(CONTENT-LENGTH,"1024");

    B.

    response.setHeader("CONTENT-LENGTH","1024");

    C.

    response.setStatus(1024);

    D.

    response.setHeader("CONTENT-LENGTH",1024);

  7. Choose the servlet code fragment that gets a binary stream for writing an image or other binary type to the HttpServletResponse.

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

  8. Which methods are used by a servlet to handle form data from a client? (Choose all that apply.)

    A.

    HttpServlet.doHead()

    B.

    HttpServlet.doPost()

    C.

    HttpServlet.doForm()

    D.

    ServletRequest.doGet()

    E.

    ServletRequest.doPost()

    F.

    ServletRequest.doForm()

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

    A.

    getMethod()

    B.

    getHeader()

    C.

    getCookies()

    D.

    getInputStream()

    E.

    getParameterNames()

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

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

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.