Coffee Cram: Chapter 13 Answers

  1. Which are true about filters? (Choose all that apply.)

    (Servlet v2.4 section 6)

    A.

    A filter can act on only the request or response object, not both.

    B.

    The destroy method is always a container callback method.

    C.

    The doFilter method is always a container callback method.

    D.

    The only way a filter can be invoked is through a declaration in the DD.

    E.

    The next filter in a filter chain can be specified either by the previous filter or in the DD.

    Note

    -Option C is incorrect, doFilter is both a callback and an inline method.

    -Option E is incorrect, the order of filter execution is always determined in the DD.

  2. Which are true about declaring filters in the DD? (Choose all that apply.)

    (Servlet v2.4 section 6)

    A.

    Unlike servlets, filters CANNOT declare initialization parameters.

    B.

    Filter chain order is always determined by the order the elements appear in the DD.

    C.

    A class that extends an API request or response wrapper class must be declared in the DD.

    D.

    A class that extends an API request or response wrapper class is using the Intercepting Filter pattern.

    E.

    Filter chain order is affected by whether filter mappings are declared via <url-pattern> or via <servlet-name>.

    Note

    -Option B is incorrect, because <url-pattern> mappings will be chained before <servlet-name> mappings.

    -Option D is incorrect, wrappers are examples of the Decorator pattern.

  3. Given the class UserRequest is an implementation of HttpServletRequest, and given that this method in an otherwise properly defined Filter implementation:

    (Servlet v2.4 pg. 49)

    20. public void doFilter(ServletRequest req,
    21.                        ServletResponse response,
    22.                        FilterChain chain)
    22.        throws IOException, ServletException {
    23.   HttpServletRequest request = (HttpServletRequest) req;
    23.   HttpSession session = request.getSession();
    25.   Object user = session.getAttribute("user");
    26.   if (user != null) {
    27.     UserRequest ureq = new UserRequest(request, user);
    28.     chain.doFilter(ureq, response);
    29.   } else {
    30.     RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
    31.     rd.forward(request, response);
    32.   }
    33. }

    Which is true?

    A.

    An exception will always be thrown if line 31 executes.

    B.

    Line 28 is invalid because request must be passed as the first argument.

    C.

    This line: chain.doFilter(request, response) must be inserted somewhere in the else block.

    D.

    This method does not properly implement Filter.doFilter() because the method signature is incorrect.

    E.

    None of the above.

    Note

    -Option A is incorrect as it is valid for a filter to forward a request.

    -Option B is incorrect because it is valid for a filter to wrap a request (note that UserRequest must implement ServletRequest).

    -Option C is incorrect because the doFilter method is NOT required to call chain.doFilter().

    -Option D is incorrect because the method signature is correct.

  4. Given a partial deployment descriptor:

    (Servlet v2.4 pg. 53)

    11.  <filter>
    12.     <filter-name>My Filter</filter-name>
    13.     <filter-class>com.example.MyFilter</filter-class>
    14.  </filter>
    15.  <filter-mapping>
    16.      <filter-name>My Filter</filter-name>
    17.      <url-pattern>/my</url-pattern>
    18. </filter-mapping>
    19. <servlet>
    20.   <servlet-name>My Servlet</servlet-name>
    21.   <servlet-class>com.example.MyServlet</servlet-
    class>
    22.  </servlet>
    23.  <servlet-mapping>
    24.    <servlet-name>My Servlet</servlet-name>
    25.    <url-pattern>/my</url-pattern>
    26.  </servlet-mapping>

    Which is true? (Choose all that apply.)

    A.

    The file is invalid because the URL pattern /my is mapped to both a servlet and a filter.

    B.

    The file is invalid because neither the servlet name nor the filter name is allowed to contain spaces.

    C.

    The filter MyFilter will be invoked after the MyServlet servlet for each request that matches the pattern /my.

    D.

    The filter MyFilter will be invoked before the MyServlet servlet for each request that matches the pattern /my.

    E.

    The file is invalid because the <filter> element must contain a <servlet-name> element that defines which servlet the filter should be applied to.

    Note

    -Option A is incorrect because this is proper syntax used to map a filter to the same pattern as a servlet.

    -Option B is incorrect because there is no such restriction.

    -Option C is incorrect because filters are executed before servlets, not after.

    -Option E is incorrect because either a <servlet-name> element or a <url-pattern> may be used within a <filter-mapping> element.

  5. Which about filters are true? (Choose all that apply.)

    (Servlet v2.4 pg. 51)

    A.

    Filters may be used to create request or response wrappers.

    B.

    Wrappers may be used to create request or response filters.

    C.

    Unlike servlets, all filter initialization code should be placed in the constructor since there is no init() method.

    D.

    Filters support an initialization mechanism that includes an init() method that is guaranteed to be called before the filter is used to handle requests.

    E.

    A filter’s doFilter() method must call doFilter() on the input FilterChain object in order to ensure that all filters have a chance to execute.

    F.

    When calling doFilter() on the input FilterChain, a filter’s doFilter() method must pass in the same ServletRequest and ServletResponse objects that were passed into it.

    G.

    A filter’s doFilter() may block further request processing.

    Note

    -Option B is incorrect because the terminology is reversed.

    -Option C is incorrect because there is an init() method that should be used for filter initialization.

    -Option E is incorrect because calling doFilter() is not necessary if a filter wishes to block further request processing.

    -Option F is incorrect because the filter may choose to “wrap” the request or the response object and pass those instead.

  6. Which are true about the servlet Wrapper classes? (Choose all that apply.)

    (API)

    A.

    They provide the only mechanism for wrapping ServletResponse objects.

    B.

    They can be used to decorate classes that implement Filter.

    C.

    They can be used even when the application does NOT support HTTP.

    D.

    The API provides wrappers for ServletRequest, ServletResponse, and FilterChain objects.

    E.

    They implement the Intercepting Filter pattern.

    F.

    When you subclass a wrapper class, you must override at least one of the wrapper class’s methods.

    Note

    -Option A is incorrect because you can create your own wrapper class.

    -Option B is incorrect because these classes are used to wrap requests and responses.

    -Option D is incorrect because the API does NOT provide a FilterChain wrapper.

    -Option E is incorrect because these wrappers implement the Decorator pattern..

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

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