Coffee Cram: Chapter 5 Answers

  1. When using a RequestDispatcher, the use of which methods can often lead to an IllegalStateException? (Choose all that apply.)

    (Servlet v2.4 pg. 167)

    A.

    read

    B.

    flush

    C.

    write

    D.

    getOutputStream

    E.

    getResourceAsStream

    Note

    -An IllegalStateException is caused when a response has already been ‘committed’ to the client (the flush method does that), and then you attempt a forward.

  2. Which statements about ServletContext initialization parameters are true? (Choose all that apply.)

    (Servlet v2.4 pg. 31)

    A.

    They should be used for data that changes rarely.

    B.

    They should be used for data that changes frequently.

    C.

    They can be accessed using

    ServletContext.getParameter(String).

    D.

    They can be accessed using

    ServletContext.getInitParameter(String).

    E.

    They should be used for data that is specific to a particular servlet.

    F.

    They should be used for data that is applicable to an entire web application.

    Note

    -Option B is incorrect because ServletContext init parameters are only read at Container start-up time.

    -Option C is incorrect because this method does not exist.

    -Option E is incorrect because there is only one ServletContext object per web application.

  3. Which types define the methods getAttribute() and setAttribute()? (Choose all that apply.)

    (Servlet v2.4 pgs. 32, 36, 59)

    A.

    HttpSession

    B.

    ServletRequest

    C.

    ServletResponse

    D.

    ServletContext

    E.

    ServletConfig

    F.

    SessionConfig

  4. If a servlet is invoked using the forward or include method of RequestDispatcher, which methods of the servlet’s request object can access the request attributes set by the container? (Choose all that apply.)

    (Servlet v2.4 65-66)

    A.

    getCookies

    B.

    getAttribute

    C.

    getRequestPath

    D.

    getRequestAttribute

    E.

    getRequestDispatcher

    Note

    -Option B is the correct method. With it you can access the container populated javax.servlet.forward.Xxx and javax.servlet.include.Xxxx attributes.

    -Options C and D refer to methods that don’t exist.

  5. Which calls provide information about initialization parameters that are applicable to an entire web application? (Choose all that apply.)

    (Servlet v2.4 pg. 32)

    A.

    ServletConfig.getInitParameters()

    B.

    ServletContext.getInitParameters()

    C.

    ServletConfig.getInitParameterNames()

    D.

    ServletContext.getInitParameterNames()

    E.

    ServletConfig.getInitParameter(String)

    F.

    ServletContext.getInitParameter(String)

    Note

    -Options A and B are incorrect because these methods do not exist.

    -Options C and E are incorrect because they provide access to servlet-specific initialization parameters.

  6. Which statements about listeners are true? (Choose all that apply.)

    (Servlet v2.4 pg. 80)

    A.

    A ServletResponseListener can be used to perform an action when a servlet response has been sent.

    B.

    An HttpSessionListener can be used to perform an action when an HttpSession has timed out.

    C.

    A ServletContextListener can be used to perform an action when the servlet context is about to be shut down.

    D.

    A ServletRequestAttributeListener can be used to perform an action when an attribute has been removed from a ServletRequest.

    E.

    A ServletContextAttributeListener can be used to perform an action when the servlet context has just been created and is available to service its first request.

    Note

    -Option A is incorrect because these is no ServletResponseListener interface.

    -Option E is incorrect because a ServletContextListener would be used for this purpose.

  7. Which is most logically stored as an attribute in session scope?

    (Servlet v2.4 pg. 58)

    A.

    A copy of a query parameter entered by a user.

    B.

    The result of a database query to be returned immediately to a user.

    C.

    A database connection object used by all web components of the system.

    D.

    An object representing a user who has just logged into the system.

    E.

    A copy of an initialization parameter retrieved from a ServletContext object.

    Note

    -Option A is incorrect because a query parameter is more typically used immediately to perform an operation.

    -Option B is incorrect because such data is typically either immediately returned or stored in request scope.

    -Option C is incorrect because (since it is not specific to a particular session) it should be stored in context scope.

    -Option E is incorrect because servlet context parameters should stay with the ServletContext object.

  8. Given this code from an otherwise valid HttpServlet that has also been registered as a ServletRequestAttributeListener:

    (Servlet v2.4 pg. 199-200)

    10. public void doGet(HttpServletRequest req,
                            HttpServletResponse res)
    11.         throws IOException, ServletException {
    12.   req.setAttribute("a", "b");
    13.   req.setAttribute("a", "c");
    14.   req.removeAttribute("a");
    15. }
    16. public void attributeAdded(ServletRequestAttributeEvent ev) {
    17.   System.out.print(" A:" + ev.getName() + "->" + ev.getValue());
    18. }
    19. public void attributeRemoved(ServletRequestAttributeEvent ev) {
    20.   System.out.print(" M:" + ev.getName() + "->" + ev.getValue());
    21. }
    22. public void attributeReplaced(ServletRequestAttributeEvent ev) {
    23.   System.out.print(" P:" + ev.getName() + "->" + ev.getValue());
    24. }

    What logging output is generated?

    A.

    A:a->b P:a->b

    B.

    A:a->b M:a->c

    C.

    A:a->b P:a->b M:a->c

    D.

    A:a->b P:a->b P:a->null

    E.

    A:a->b M:a->b A:a->c M:a->c

    F.

    A:a->b M:a->b A:a->c P:a->null

    Note

    -Tricky! The getValue method returns the OLD value of the attribute if the attribute was replaced.

  9. When declaring a listener in the DD, which sub-elements of the <listener> element are required? (Choose all that apply.)

    (Servlet v2.4 section 10.4, & 13.4.9 )

    A.

    <description>

    B.

    <listener-name>

    C.

    <listener-type>

    D.

    <listener-class>

    E.

    <servlet-mapping>

    Note

    -The <listener-class> sub-element is the ONLY required sub-element of the <listener> element.

  10. Which types of objects can store attributes? (Choose all that apply.)

    (API)

    A.

    ServletConfig

    B.

    ServletResponse

    C.

    RequestDispatcher

    D.

    HttpServletRequest

    E.

    HttpSessionContext

    Note

    -Options A, B, and C are invalid because these types do not store attributes.

    -Option E is invalid because there is no such type.

    Note

    Note: The other two types related to servlets, that can store attributes are HttpSession and ServletContext.

  11. Which are true? (Choose all that apply.)

    (Servlet v2.4 pgs. 81-84)

    A.

    When a web application is preparing to shutdown, the order of listener notification is not guaranteed.

    B.

    When listener-friendly events occur, listener invocation order is not predictable.

    C.

    The container registers listeners based on declarations in the deployment descriptor.

    D.

    Only the container can invalidate a session.

    Note

    -Options A and B are incorrect because the container uses the DD to determine the notification order of registered listeners.

    -Option D is incorrect because a servlet can invalidate a session using the HttpSession.invalidate() method.

  12. Which statements about RequestDispatcher are true (where applicable, assume the RequestDispatcher was not obtained via a call to getNamedDispatcher())? (Choose all that apply.)

    (Servlet v2.4 pg. 65)

    A.

    A RequestDispatcher can be used to forward a request to another servlet.

    B.

    The only method in the RequestDispatcher interface is forward().

    C.

    Parameters specified in the query string used to create a RequestDispatcher are not forwarded by the forward() method.

    D.

    The servlet to which a request is forwarded may access the original query string by calling getQueryString() on the HttpServletRequest.

    E.

    The servlet to which a request is forwarded may access the original query string by calling getAttribute("javax.servlet.forward.query_string") on the ServletRequest.

    Note

    -Option B is incorrect because the interface also contains an include method.

    -Option C is incorrect because such parameters are forwarded in this case.

    -Option D is incorrect because this method returns the query string on the URL pattern from the RequestDispatcher.

  13. What is the recommended way to deal with servlets and thread safety?

    (Servlet spec p 27)

    A.

    Write the servlet code to extend ThreadSafeServlet.

    B.

    Have the servlet implement SingleThreadModel.

    C.

    Log all servlet method calls.

    D.

    Use local variables exclusively, and if you have to use instance variables, synchronize access to them.

    Note

    -Option A and B are incorrect because ThreadSafeServlet does not exist in the Servlet API and the .SingleThreadModel is deprecated in version 2.4 and not recommended..

  14. Given the following methods:

    (API)

    - getCookies

    - getContextPath

    - getAttribute

    Match the methods above to the following classes or interfaces. Note that each method can be used more than once.

    image with no caption

    Note

    At this point this shouldn’t really be about memorization as much as about what methods would make sense in each scope.

  15. Which are true about the RequestDispatcher interface? (Choose all that apply.)

    (API)

    A.

    Of its two methods, forward() is used most frequently.

    B.

    Its methods take the following arguments: a resource, a request, and a response.

    C.

    Depending on the class whose method creates a RequestDispatcher, the path to the resource to be forwarded to may change.

    D.

    Regardless of the class whose method creates a RequestDispatcher, the path to the resource to be forwarded to will NOT change.

    E.

    If your servlet invokes RequestDispatcher.forward, it can send its own response to the client before, but not after the invocation of forward.

    Note

    -Option B: the resource is specified at object creation time.

    -Option E: if your servlet uses an RD, it can never send its own response.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset