422 CHAPTER 10 INTRODUCTION TO FILTERING Listing (Web design seattle)
422 CHAPTER 10 INTRODUCTION TO FILTERING Listing 10-1. SimpleFilter.java package com.apress.projsp.filters; import java.io.*; import javax.servlet.*; public final class SimpleFilter implements Filter { private FilterConfig filterConfig = null; public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { filterConfig.getServletContext().log(”in SimpleFilter”); chain.doFilter(request, response); filterConfig.getServletContext().log(”Getting out of SimpleFilter”); } public void init(FilterConfig filterConfig) { this.filterConfig = filterConfig; } public void destroy() {} } As mentioned before, all filters must implement the javax.servlet.Filter interface, and the SimpleFilter is no exception. The filter does its work in the doFilter() method. Note the parameters: a request, response, and a FilterChain object. However, before the container calls doFilter(), it will pass a FilterConfigobject to the filter through the init()method. If the filter needs to access any resources from the FilterConfig object, the class must save a reference to that object. To write to the log, you access the ServletContext from FilterConfigas provided by the container. The log() method will write a line by using the logger set up earlier for this context. The rest of the methods are standard trivial implementations required for the Filter interface. Declaring the Filter and Configuring Filter Mapping Now, you need to add a
Check Tomcat Web Hosting services for best quality webspace to host your web application.