SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 96 | Next

Larry Brown, Marty Hall, and Yaakov Chaikin

"Core Servlets and JavaServer Pages, Volume 2"

To access the output of the servlet or
JSP page, a filter can wrap the response object inside a stand-in object that, for example,
accumulates the output into a buffer. After the call to the doFilter method of
the FilterChain object, the filter can examine the buffer, modify it if necessary, and
then pass it on to the client.
For example, Listing 2.15 defines a simple filter that intercepts requests and
prints a report on the standard output (available with most servers when you run
them on your desktop during development) whenever the associated servlet or JSP
page is accessed.
Listing 2.15 ReportFilter.java
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
/** Simple filter that prints a report on the standard output
* whenever the associated servlet or JSP page is accessed.
*/
public class ReportFilter implements Filter {
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest)request;
System.out.println(req.getRemoteHost() +
" tried to access " +
req.getRequestURL() +
" on " + new Date() + ".");
chain.doFilter(request,response);
}
public void init(FilterConfig config)
throws ServletException {
}
public void destroy() {}
}
Chapter 2 ?–  Controlling Web Application Behavior with web.xml 70
Once you have created a filter, you declare it in the web.


Pages:
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108