Class HttpFilter
- All Implemented Interfaces:
Serializable
,Filter
,FilterConfig
Provides an abstract class to be subclassed to create
an HTTP filter suitable for a Web site. A subclass of
HttpFilter
should override doFilter(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
.
Filters typically run on multithreaded servers, so be aware that a filter must handle concurrent requests and be careful to synchronize access to shared resources. Shared resources include in-memory data such as instance or class variables and external objects such as files, database connections, and network connections. See the Java Tutorial on Multithreaded Programming for more information on handling multiple threads in a Java program.
- Since:
- Servlet 4.0
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
doFilter
(HttpServletRequest req, HttpServletResponse res, FilterChain chain) ThedoFilter
method 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.void
doFilter
(ServletRequest req, ServletResponse res, FilterChain chain) ThedoFilter
method 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.Methods inherited from class javax.servlet.GenericFilter
getFilterConfig, getFilterName, getInitParameter, getInitParameterNames, getServletContext, init, init
-
Constructor Details
-
HttpFilter
public HttpFilter()Does nothing, because this is an abstract class.
- Since:
- 4.0
-
-
Method Details
-
doFilter
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException The
doFilter
method 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. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain. There's no need to override this method.The default implementation inspects the incoming
req
andres
objects to determine if they are instances ofHttpServletRequest
andHttpServletResponse
, respectively. If not, aServletException
is thrown. Otherwise, the protecteddoFilter(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
method is called.- Parameters:
req
- aServletRequest
object that contains the request the client has made of the filterres
- aServletResponse
object that contains the response the filter sends to the clientchain
- theFilterChain
for invoking the next filter or the resource- Throws:
IOException
- if an input or output error is detected when the filter handles the requestServletException
- if the request for the could not be handled or either parameter is not an instance of the respectiveHttpServletRequest
orHttpServletResponse
.- Since:
- Servlet 4.0
- See Also:
-
doFilter
protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException The
doFilter
method 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. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.The default implementation simply calls
FilterChain.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
- Parameters:
req
- aHttpServletRequest
object that contains the request the client has made of the filterres
- aHttpServletResponse
object that contains the response the filter sends to the clientchain
- theFilterChain
for invoking the next filter or the resource- Throws:
IOException
- if an input or output error is detected when the filter handles the requestServletException
- if the request for the could not be handled- Since:
- Servlet 4.0
-