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 | |
C. | The | |
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. |
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 |
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 | |
C. | This line: | |
D. | This method does not properly implement | |
E. | None of the above. |
-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.
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 | |
B. | The file is invalid because neither the servlet name nor the filter name is allowed to contain spaces. | |
C. | The filter | |
D. | The filter | |
E. | The file is invalid because the |
-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.
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 | |
D. | Filters support an initialization mechanism that includes an | |
E. | A filter’s | |
F. | When calling | |
G. | A filter’s |
-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.
Which are true about the servlet Wrapper classes? (Choose all that apply.)
(API)
A. | They provide the only mechanism for wrapping | |
B. | They can be used to decorate classes that implement | |
C. | They can be used even when the application does NOT support HTTP. | |
D. | The API provides wrappers for | |
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. |
-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..