ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzamy_5.4.0.1/50/program/servfilt.htm

102 lines
4.4 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Servlet filtering</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h4><a name="servfilt"></a>Servlet filtering</h4>
<p>Servlet filtering is an integral part of the Servlet API. Servlet filtering provides a new type of object called a filter that can transform a request or modify a response.</p>
<p>You can chain filters together so that a group of filters can act on the input and output of a specified resource or group of resources.</p>
<p>Filters typically include logging filters, image conversion filters, encryption filters, and Multipurpose Internet Mail Extensions (MIME) type filters, which are functionally equivalent to the servlet chaining. Although filters are not servlets, their lifecycle is very similar.</p>
<p>Filters are handled in the following manner:</p>
<ul>
<li><p>The Web container determines whether it needs to construct a FilterChain that contains the LoggingFilter for the requested resource. The FilterChain begins with the invocation of the LoggingFilter and ends with the invocation of the requested resource.</p></li>
<li><p>If other filters need to go in the chain, the Web container places them after the LoggingFilter and before the requested resource.</p></li>
<li><p>The Web container then instantiates and initializes the LoggingFilter (if it was not done previously) and invokes its doFilter(FilterConfig) method to start the chain.</p></li>
<li><p>The LoggingFilter preprocesses the request and response objects and then invokes the filter chain doFilter(ServletRequest, ServletResponse) method. This method passes the processing to the next resource in the chain (in this case, the requested resource).</p></li>
<li><p>Upon return from the filter chain doFilter(ServletRequest, ServletResponse) method, the LoggingFilter performs post-processing on the request and response object before sending the response back to the client.</p></li>
</ul>
<p><strong>Filter, FilterChain, FilterConfig classes for servlet filtering</strong></p>
<p>The following interfaces are defined as part of the javax.servlet package:</p>
<ul>
<li>Filter interface: doFilter(), getFilterConfig(), and setFilterConfig() methods</li>
<li>FilterChain interface: doFilter() method</li>
<li>FilterConfig interface: getFilterName(), getInitParameter(), getInitParameterNames(), and getServletContext() methods</li>
</ul>
<p>The following classes are defined as part of the javax.servlet.http package (see the J2EE 1.3 documentation for information about methods):</p>
<ul>
<li><a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequestWrapper.html" target="_">HttpServletRequestWrapper</a> <img src="www.gif" width="18" height="15" alt="Link outside Information Center"></li>
<li><a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletResponseWrapper.html" target="_">HttpServletResponseWrapper</a> <img src="www.gif" width="18" height="15" alt="Link outside Information Center"></li>
</ul>
<p><strong>Example: com.ibm.websphere.LoggingFilter.java</strong></p>
<p>The following example shows how to implement a filter:</p>
<pre>package com.ibm.websphere;
import java.io.*;
import javax.servlet.*;
public class LoggingFilter implements Filter {
File _loggingFile = null;
// implement the required init method
public void init(FilterConfig fc) {
// create the logging file
...
}
// implement the required doFilter method.
// this is where most of the work is done
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) {
try {
// add request info to the log file
synchronized(_loggingFile) {
...
}
// pass the request on to the next resource in the chain
chain.doFilter(request, response);
}
catch (Throwable t) {
// handle problem...
}
}
// implement the required destroy method
public void destroy() {
// make sure logging file is closed
_loggingFile.close();
}
}</pre>
</body>
</html>