FileDocCategorySizeDatePackage
ExampleFilter.javaAPI DocGlassfish v2 API5360Fri May 04 22:34:22 BST 2007filters

ExampleFilter

public final class ExampleFilter extends Object implements Filter
Example filter that can be attached to either an individual servlet or to a URL pattern. This filter performs the following functions:
  • Attaches itself as a request attribute, under the attribute name defined by the value of the attribute initialization parameter.
  • Calculates the number of milliseconds required to perform the servlet processing required by this request, including any subsequently defined filters, and logs the result to the servlet context log for this application.
author
Craig McClanahan
version
$Revision: 1.3 $ $Date: 2007/05/05 05:34:22 $

Fields Summary
private String
attribute
The request attribute name under which we store a reference to ourself.
private FilterConfig
filterConfig
The filter configuration object we are associated with. If this value is null, this filter instance is not currently configured.
Constructors Summary
Methods Summary
public voiddestroy()
Take this filter out of service.



    // --------------------------------------------------------- Public Methods


               
       

        this.attribute = null;
        this.filterConfig = null;

    
public voiddoFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
Time the processing that is performed by all subsequent filters in the current filter stack, including the ultimately invoked servlet.

param
request The servlet request we are processing
param
result The servlet response we are creating
param
chain The filter chain we are processing
exception
IOException if an input/output error occurs
exception
ServletException if a servlet error occurs


	// Store ourselves as a request attribute (if requested)
	if (attribute != null)
	    request.setAttribute(attribute, this);

	// Time and log the subsequent processing
	long startTime = System.currentTimeMillis();
        chain.doFilter(request, response);
	long stopTime = System.currentTimeMillis();
	filterConfig.getServletContext().log
	    (this.toString() + ": " + (stopTime - startTime) +
	     " milliseconds");

    
public voidinit(javax.servlet.FilterConfig filterConfig)
Place this filter into service.

param
filterConfig The filter configuration object


	this.filterConfig = filterConfig;
        this.attribute = filterConfig.getInitParameter("attribute");

    
public java.lang.StringtoString()
Return a String representation of this object.


	if (filterConfig == null)
	    return ("InvokerFilter()");
	StringBuffer sb = new StringBuffer("InvokerFilter(");
	sb.append(filterConfig);
	sb.append(")");
	return (sb.toString());