Compile an Implementation Program
The filter API contains three simple interfaces which are neatly nested in the javax. servlet package. The three interfaces are Filter, Filter Chain, and Filter Config. From the programming point of view, the Filter class will implement the Filter interface, and then use the Filter Chain and Filter Config interfaces in this Filter class. A reference of the Filter class is passed to the Filter Chain object, allowing the Filter to pass control to the next resource in the Chain. The Filter Config object is provided to the Filter by the container to allow access to the initialization data of the Filter.
To be consistent with our three-step mode, the Filter must use three methods to fully implement the Filter interface:
Init (): This method is called when the container instantiates a filter. It is mainly designed to prepare the filter for processing. This method accepts an object of the Filter Config type as input.
DoFilter (): Same as servlet which has a service () method, this method calls doPost () or doGet () to process requests, the filter has a single method for processing requests and responses-The doFilter method. This method accepts three input parameters: a Servlet Request, response, and a Filter Chain object.
Destroy (): As you imagine, this method performs any cleanup operations that may need to be performed before automatic garbage collection.
Listing 1 shows a very simple filter that tracks the approximate time taken to satisfy a client's Web requests.
List 1. Implement a filter class
- import javax.servlet.*;
- import java.util.*;
- import java.io.*;
- public class TimeTrackFilter implements Filter {
- private FilterConfig filterConfig = null;
- public void init(FilterConfig filterConfig) throws ServletException {
- this.filterConfig = filterConfig;
- }
- public void destroy() {
- this.filterConfig = null;
- }
- public void doFilter( ServletRequest request, ServletResponse response,
FilterChain chain ) throws IOException, ServletException {
- Date startTime, endTime;
- double totalTime;
- startTime = new Date(); // Forward the request to the next resource in
the chain chain.doFilter(request, wrapper);
- // -- Process the response -- \\
- // Calculate the difference between the start time and end time
- endTime = new Date();
- totalTime = endTime.getTime() - startTime.getTime();
- totalTimetotalTime = totalTime / 1000; //Convert from milliseconds to
seconds StringWriter sw = new StringWriter();
- PrintWriter writer = new PrintWriter(sw);
- writer.println();
- writer.println("===============");
- writer.println("Total elapsed time is: " + totalTime + " seconds." );
writer.println("==============="); // Log the resulting string writer.flush();
filterConfig.getServletContext(). log(sw.getBuffer().toString());
- }
- }
The lifecycle of this filter is very simple. In any case, let's take a look at it:
Initialization
When the container loads the filter for the first time, the init () method is called. This class contains a reference pointing to the Filter Config object in this method. Our filter does not actually need to do this, because initialization information is not used here, just for demonstration purposes.
Filter
Most of the time consumed by the filter is here. The doFilter method is called by the container and is passed in to reference Servlet Request, Servlet Response, and Filter Chain objects in the Request/Response Chain respectively. The Filter then has the opportunity to process the request, pass the processing task to the next resource in the Chain by calling the doFilter method referenced by the Filter Chain object), and then process the response when the processing control returns the Filter.
Structure Analysis
The container immediately calls the destroy () method before garbage collection to execute any necessary cleanup code.
- Servlet Container matching process
- How to call Servlet to process requests
- Extended Future Response Servlet
- Three methods for compiling Servlet
- Transactions in Servlet containers