What is the usage of Filter
Filters can perform many different types of functions like:
- 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. You can have your filter to GZIP the response and send it to the browser if the http request indicated that the Web browser [Accept-Encoding : gzip]. (detail)
Code to obtain Accept-Encoding header
// Get the "Accepting-Encoding" header from the HTTP request.
// Note that request is an HttpServletRequest instance
String encoding = request.getHeader("Accept-Encoding");Â Â
 boolean supportsGzip =false;
 // Now, check to see if the browser supports the GZIP compression
 if (encoding != null) {
   if (encoding.toLowerCase().indexOf("gzip") > -1)
     supportsGzip = true;
 }
Code to GZIP the response
 // Set the response header.
 // Note that response is an HttpResponse instance.
 response.setHeader("Content-Encoding", "gzip");
 // Assuming you have stuffed all the HTML data
 // in a StringBuffer sb, let's pump everything
 // in the StringBuffer to the ServletResponse's
 // OutputStream via the GZIPOutputStream
 try {
   // remember to import java.util.zip.GZIPOutputStream
   GZIPOutputStream gzos =
        new GZIPOutputStream(response.getOutputStream());
   gzos.write(sb.toString().getBytes());
   gzos.close();
 } catch(IOException ie) {
   // handle the error here
   ...
 }
- Localization-Targeting the request and response to a particular locale.
- XSL/T transformations of XML content-Targeting web application responses to more than one type of client.
- Encryption
- Tokenizing
- Triggering resource access events
- Mime-type chaining
- Caching
The filter API is defined by the Filter, FilterChain, and FilterConfig interfaces in the javax.servlet package. You define a filter by implementing the Filter interface. A filter chain, passed to a filter by the container, provides a mechanism for invoking a series of filters. A filter config contains initialization data.Â
Example of usage – character encoding
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
 /**
  * The filter configuration object we are associated with. If this value
  * is null, this filter instance is not currently configured.
  */
 protected FilterConfig filterConfig = null;
 /**
  * Should a character encoding specified by the client be ignored?
  */
 protected boolean ignore = true; public void destroy() {
   this.encoding = null;
   this.filterConfig = null;
 } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
     ServletException {
   // Conditionally select and set the character encoding to be used
   if (ignore || (request.getCharacterEncoding() == null)) {
     String localEncoding = selectEncoding(request);
     if (localEncoding != null)
       request.setCharacterEncoding(localEncoding);
   }   // Pass control on to the next filter
   chain.doFilter(request, response);
 }
 public void init(FilterConfig localFilterConfig) throws ServletException {
   this.filterConfig = localFilterConfig;
   this.encoding = filterConfig.getInitParameter("encoding");
   String value = filterConfig.getInitParameter("ignore");
   if (value == null)
     this.ignore = true;
   else if (value.equalsIgnoreCase("true"))
     this.ignore = true;
   else if (value.equalsIgnoreCase("yes"))
     this.ignore = true;
   else
     this.ignore = false;
 }
 protected String selectEncoding(ServletRequest request) {
   return (this.encoding);
 }
Reference
Here is the link that has the gzip filter implementation and the test results to have it enable for flex httpservice, webservice and remoteobject. Look like it only benefits when the size of the file is > 100K. (blog)
0 Comments.