Class GenericFilter
- java.lang.Object
-
- com.twelvemonkeys.servlet.GenericFilter
-
- All Implemented Interfaces:
java.io.Serializable,javax.servlet.Filter,javax.servlet.FilterConfig
- Direct Known Subclasses:
BrowserHelperFilter,CacheFilter,FileUploadFilter,GZIPFilter,ImageFilter,ThrottleFilter,TimingFilter,TrimWhiteSpaceFilter
public abstract class GenericFilter extends java.lang.Object implements javax.servlet.Filter, javax.servlet.FilterConfig, java.io.SerializableDefines a generic, protocol-independent filter.GenericFilteris inspired byGenericServlet, and implements theFilterandFilterConfiginterfaces.GenericFiltermakes writing filters easier. It provides simple versions of the lifecycle methodsinitanddestroyand of the methods in theFilterConfiginterface.GenericFilteralso implements thelogmethods, declared in theServletContextinterface.GenericFilterhas an auto-init system, that automatically invokes the method matching the signaturevoid setX(<Type>), for every init-parameterx. Both camelCase and lisp-style parameter naming is supported, lisp-style names will be converted to camelCase. Parameter values are automatically converted from string representation to most basic types, if necessary.To write a generic filter, you need only override the abstract
doFilterImpl(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)doFilterImpl} method.- Version:
- $Id: GenericFilter.java#1 $
- Author:
- Harald Kuhr, last modified by $Author: haku $
- See Also:
Filter,FilterConfig, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanoncePerRequestIndicates if this filter should run once per request (true), or for each forward/include resource (false).
-
Constructor Summary
Constructors Constructor Description GenericFilter()Does nothing.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voiddestroy()Called by the web container to indicate to a filter that it is being taken out of service.voiddoFilter(javax.servlet.ServletRequest pRequest, javax.servlet.ServletResponse pResponse, javax.servlet.FilterChain pFilterChain)ThedoFiltermethod of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.protected abstract voiddoFilterImpl(javax.servlet.ServletRequest pRequest, javax.servlet.ServletResponse pResponse, javax.servlet.FilterChain pChain)Invoked once, or each time a request/response pair is passed through the chain, depending on theoncePerRequestmember variable.javax.servlet.FilterConfiggetFilterConfig()Gets theFilterConfigfor this filter.java.lang.StringgetFilterName()Returns the filter-name of this filter as defined in the deployment descriptor.java.lang.StringgetInitParameter(java.lang.String pKey)Returns aStringcontaining the value of the named initialization parameter, or null if the parameter does not exist.java.util.EnumerationgetInitParameterNames()Returns the names of the servlet's initialization parameters as anEnumerationofStringobjects, or an emptyEnumerationif the servlet has no initialization parameters.javax.servlet.ServletContextgetServletContext()Returns a reference to theServletContextin which the caller is executing.voidinit()A convenience method which can be overridden so that there's no need to callsuper.init(config).voidinit(javax.servlet.FilterConfig pConfig)Called by the web container to indicate to a filter that it is being placed into service.protected voidlog(java.lang.String pMessage)Writes the specified message to a servlet log file, prepended by the filter's name.protected voidlog(java.lang.String pMessage, java.lang.Throwable pThrowable)Writes an explanatory message and a stack trace for a givenThrowableto the servlet log file, prepended by the filter's name.voidsetFilterConfig(javax.servlet.FilterConfig pFilterConfig)Deprecated.For compatibility only, useinitinstead.voidsetOncePerRequest(boolean pOncePerRequest)Specifies if this filter should run once per request (true), or for each forward/include resource (false).
-
-
-
Field Detail
-
oncePerRequest
protected boolean oncePerRequest
Indicates if this filter should run once per request (true), or for each forward/include resource (false).Set this variable to true, to make sure the filter runs once per request.
NOTE: As of Servlet 2.4, this field should always be left to it's default value (
false).
To run the filter once per request, thefilter-mappingelement of the web-descriptor should include adispatcherelement:<dispatcher>REQUEST</dispatcher>
-
-
Method Detail
-
init
public void init(javax.servlet.FilterConfig pConfig) throws javax.servlet.ServletExceptionCalled by the web container to indicate to a filter that it is being placed into service.This implementation stores the
FilterConfigobject it receives from the servlet container for later use. Generally, there's no reason to override this method, override the no-argumentinitinstead. However, if you are overriding this form of the method, always callsuper.init(config).This implementation will also set all configured key/value pairs, that have a matching setter method annotated with
InitParam.- Specified by:
initin interfacejavax.servlet.Filter- Parameters:
pConfig- the filter config- Throws:
javax.servlet.ServletException- if an error occurs during init- See Also:
Filter.init(javax.servlet.FilterConfig),init,BeanUtil.configure(Object, java.util.Map, boolean)
-
init
public void init() throws javax.servlet.ServletExceptionA convenience method which can be overridden so that there's no need to callsuper.init(config).- Throws:
javax.servlet.ServletException- if an error occurs during init- See Also:
init(FilterConfig)
-
doFilter
public final void doFilter(javax.servlet.ServletRequest pRequest, javax.servlet.ServletResponse pResponse, javax.servlet.FilterChain pFilterChain) throws java.io.IOException, javax.servlet.ServletExceptionThedoFiltermethod of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.Subclasses should not override this method, but rather the abstract
doFilterImpl(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)doFilterImpl} method.- Specified by:
doFilterin interfacejavax.servlet.Filter- Parameters:
pRequest- the servlet requestpResponse- the servlet responsepFilterChain- the filter chain- Throws:
java.io.IOExceptionjavax.servlet.ServletException- See Also:
Filter.doFilter,doFilterImpl
-
doFilterImpl
protected abstract void doFilterImpl(javax.servlet.ServletRequest pRequest, javax.servlet.ServletResponse pResponse, javax.servlet.FilterChain pChain) throws java.io.IOException, javax.servlet.ServletExceptionInvoked once, or each time a request/response pair is passed through the chain, depending on theoncePerRequestmember variable.- Parameters:
pRequest- the servlet requestpResponse- the servlet responsepChain- the filter chain- Throws:
java.io.IOException- if an I/O error occursjavax.servlet.ServletException- if an exception occurs during the filter process- See Also:
oncePerRequest,doFilter,Filter.doFilter
-
destroy
public void destroy()
Called by the web container to indicate to a filter that it is being taken out of service.- Specified by:
destroyin interfacejavax.servlet.Filter- See Also:
Filter.destroy()
-
getFilterName
public java.lang.String getFilterName()
Returns the filter-name of this filter as defined in the deployment descriptor.- Specified by:
getFilterNamein interfacejavax.servlet.FilterConfig- Returns:
- the filter-name
- See Also:
FilterConfig.getFilterName()
-
getServletContext
public javax.servlet.ServletContext getServletContext()
Returns a reference to theServletContextin which the caller is executing.- Specified by:
getServletContextin interfacejavax.servlet.FilterConfig- Returns:
- the
ServletContextobject, used by the caller to interact with its servlet container - See Also:
FilterConfig.getServletContext(),ServletContext
-
getInitParameter
public java.lang.String getInitParameter(java.lang.String pKey)
Returns aStringcontaining the value of the named initialization parameter, or null if the parameter does not exist.- Specified by:
getInitParameterin interfacejavax.servlet.FilterConfig- Parameters:
pKey- aStringspecifying the name of the initialization parameter- Returns:
- a
Stringcontaining the value of the initialization parameter
-
getInitParameterNames
public java.util.Enumeration getInitParameterNames()
Returns the names of the servlet's initialization parameters as anEnumerationofStringobjects, or an emptyEnumerationif the servlet has no initialization parameters.- Specified by:
getInitParameterNamesin interfacejavax.servlet.FilterConfig- Returns:
- an
EnumerationofStringobjects containing the mNames of the servlet's initialization parameters
-
log
protected void log(java.lang.String pMessage)
Writes the specified message to a servlet log file, prepended by the filter's name.- Parameters:
pMessage- the log message- See Also:
ServletContext.log(String)
-
log
protected void log(java.lang.String pMessage, java.lang.Throwable pThrowable)Writes an explanatory message and a stack trace for a givenThrowableto the servlet log file, prepended by the filter's name.- Parameters:
pMessage- the log messagepThrowable- the exception- See Also:
ServletContext.log(String,Throwable)
-
setFilterConfig
public void setFilterConfig(javax.servlet.FilterConfig pFilterConfig)
Deprecated.For compatibility only, useinitinstead.Initializes the filter.- Parameters:
pFilterConfig- the filter config- See Also:
init
-
getFilterConfig
public javax.servlet.FilterConfig getFilterConfig()
Gets theFilterConfigfor this filter.- Returns:
- the
FilterConfigfor this filter - See Also:
FilterConfig
-
setOncePerRequest
@InitParam(name="once-per-request") public void setOncePerRequest(boolean pOncePerRequest)
Specifies if this filter should run once per request (true), or for each forward/include resource (false). Called automatically from theinit-method, with settings from web.xml.- Parameters:
pOncePerRequest-trueif the filter should run only once per request- See Also:
oncePerRequest
-
-