180 likes | 239 Views
The Interceptor Pattern. Filters in the Servlet 2.3 Specification. Craig E. Ward CMSI 688 Object Technology Fall 2002 Instructor Munir Samplewala. Agenda. Description of the Interceptor Pattern Uses of the Pattern Code Samples Software Requirements Summary and Conclusions
E N D
The Interceptor Pattern Filters in the Servlet 2.3 Specification Craig E. Ward CMSI 688 Object Technology Fall 2002 Instructor Munir Samplewala
Agenda • Description of the Interceptor Pattern • Uses of the Pattern • Code Samples • Software Requirements • Summary and Conclusions • Sources and Resources • Demonstration (if practical) • Questions CMSI 688 Object Technologies
UML Class Diagram CMSI 688 Object Technologies
UML Sequence Diagram CMSI 688 Object Technologies
Flow Diagram CMSI 688 Object Technologies
Similar Patterns • Chain of Responsibility • Decorator • Front Controller • Different because… • All Interceptors may act on the request • The other patterns are “built-in” • Interceptor Filters are pluggable CMSI 688 Object Technologies
Interceptor Pattern Uses • Authentication • Access Validation • System Logging • System Debugging • Data Encoding • Browser Trapping • Response Filtering CMSI 688 Object Technologies
javax.servlet.Filter void init(FilterConfig filterConfig) void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) void destroy() CMSI 688 Object Technologies
javax.servlet.FilterChain void doFilter(ServletRequest request, ServletResponse response) CMSI 688 Object Technologies
javax.servlet.http Wrappers • javax.servlet.http.HttpServletRequestWrapper • javax.servlet.http.HttpServletResponseWrapper These classes provide a complete set of methods implementing the HttpServletRequest and HttpServletResponse classes. CMSI 688 Object Technologies
An Example of doFilter() public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)srequest; request.setCharacterEncoding(targetEncoding); // move on to the next chain.doFilter(srequest,sresponse); } CMSI 688 Object Technologies
Deployment Descriptor • New tags filter and filter-mapping <filter> <filter-name>EncodeFilter</filter-name> <display-name>EncodeFilter</display-name> <description></description> <filter-class>corepatterns.filters.encodefilter.EncodeFilter</filter-class> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> CMSI 688 Object Technologies
Software Requirements • Servlet container implementing 2.3 • Tomcat 4 • J2EE 1.3 CMSI 688 Object Technologies
Summary and Conclusions • Filters are a powerful addition to the Servlet specification • Pluggable wrappers for existing code • Add security • Add debugging • Add Logging • Greater separation of content from technology CMSI 688 Object Technologies
Sources and Resources • Sun Microsystems J2EE Blueprints • http://java.sun.com/blueprints/ • http://java.sun.com/blueprints/patterns/InterceptingFilter.html • http://developer.java.sun.com/developer/JDCTechTips/2001/tt0626.html • Li, Sing. Filtering tricks for your Tomcat. • http://www-106.ibm.com/developerworks/library/j-tomcat/index.html • Hunter, Jason, William Crawford. Java Servlet Programming, O’Reilly, 2001. CMSI 688 Object Technologies
Demonstration If time is available and the equipment can be setup, the example from “Filtering tricks for your Tomcat” will be demonstrated. CMSI 688 Object Technologies
Questions? That’s All Folks! CMSI 688 Object Technologies