Presentation is loading. Please wait.

Presentation is loading. Please wait.

Servlet Filters import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TimerFilter implements Filter { /* J2EE v1.3 Filter.

Similar presentations


Presentation on theme: "Servlet Filters import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TimerFilter implements Filter { /* J2EE v1.3 Filter."— 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 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> timerFilter TimerFilter timerFilter /* welcome WelcomeServlet welcome /welcome

3 Servlet Filters Child elements of MUST be defined in this order:

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 UpperCaseFilter The 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.


Download ppt "Servlet Filters import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TimerFilter implements Filter { /* J2EE v1.3 Filter."

Similar presentations


Ads by Google