1 / 6

Servlet Filters

Servlet Filters. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TimerFilter implements Filter { /* J2EE v1.3 Filter interface requires overriding these methods: * getFilterConfig() * setFilterConfig() * doFilter()

lbeth
Télécharger la présentation

Servlet Filters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Servlet Filters import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TimerFilter implements Filter { /* J2EE v1.3 Filter interface requires overriding these methods: * getFilterConfig() * setFilterConfig() * doFilter() * J2EE v1.4 Filter interface requires overriding these methods: * init() * destroy() * doFilter() */ private FilterConfig config = null; public void init( FilterConfig config ) throws ServletException { this.config = config; } public void destroy() { config = null; } /* Following 2 methods for backward compatibility */ public void setFilterConfig( FilterConfig config ) { /* J2EE v1.3 containers call this method when the Filter is instantiated and * pass in a FilterConfig object. When the container is done with the Filter, * it calls this method, passing in null. */ this.config = config; } public FilterConfig getFilterConfig() { return config; } public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException { long before = System.currentTimeMillis(); chain.doFilter(request, response); long after = System.currentTimeMillis(); String name = ""; if (request instanceof HttpServletRequest) { name = ((HttpServletRequest)request).getRequestURI(); } config.getServletContext().log(name + ": " + (after - before) + "ms"); } }

  2. Servlet Filters <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> <web-app> <!-- Filter elements MUST be defined before Servlet elements --> <!-- Filter definitions --> <filter> <filter-name>timerFilter</filter-name> <filter-class>TimerFilter</filter-class> </filter> <!-- Filter mappings --> <filter-mapping> <filter-name>timerFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Servlet definitions --> <servlet> <servlet-name>welcome</servlet-name> <servlet-class>WelcomeServlet</servlet-class> </servlet> <!-- Servlet mappings --> <servlet-mapping> <servlet-name>welcome</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> </web-app>

  3. Servlet Filters Child elements of <web-app> MUST be defined in this order: <display-name> <description> <distributable> <context-param> <filter> <filter-mapping> <listener> <servlet> <servlet-mapping> <session-config> <mime-mapping> <welcome-file-list> <error-page> <taglib> <resource-env-ref> <resource-ref> <security-constraint> <login-config> <security-role> <env-entry> <ejb-ref> <ejb-local-ref>

  4. Servlet Filters • Uses of Filters • Authentication - Blocking requests based on user identity. • Logging and auditing - Tracking users of a web application. • Image conversion - Scaling maps, and so on. • Data compression - Making downloads smaller. • Localization - Targeting the request and response to a particular locale. • XSL/T transformations of XML content - Targeting web application responses to more that one type of client.

  5. Servlet Filters • Next Example: UpperCase filter • What if you want to modify the response of a servlet? The UpperCaseFilter does just this, changing the response of a servlet to all uppercase.

  6. Servlet Filters • UpperCaseFilter: How it Works • class UpperCaseFilterThe doFilter() method contains code to wrap the response object with a custom implementation. Left alone, all the response data from within a servlet will be routed back to the client via the response output stream. If we want to capture the output stream and modify it, we need to buffer the response locally before it is sent back to the client. That is the purpose of the CharResponseWrapper. • class CharResponseWrapper Contains a java.io.CharArrayWriter, which will buffer all the output so that we can modify it after the servlet has executed. The CharResponseWrapper class extends javax.servlet.http.HttpServletResponseWrapper, which simply proxies each of the method calls made on the wrapper to the response object supplied in the constructor. Within the new response wrapper, we create a new CharArrayWriter when the response getWriter() method is called. Now, instead of the servlet response being written to the client, it will be buffered locally. We can grab a copy of the buffer by calling the toString() method on the response wrapper, which was overridden to return the String representation of the buffer. It is then an easy matter to call String.toUpperCase() and then dump the entire contents of the buffer to the *real* output stream of the original response object. Making a request to any servlet that uses getWriter() to create content will result in the response being returned in uppercase.

More Related