Welcome! [www.web

Download Report

Transcript Welcome! [www.web

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");
}
}
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>
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>
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.
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.
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.