Package org.springframework.web.filter

# Class RequestContextFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
org.springframework.web.filter.RequestContextFilter

All Implemented Interfaces:
  `Filter`, `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanNameAware`, `org.springframework.beans.factory.DisposableBean`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.context.EnvironmentAware`, `org.springframework.core.env.EnvironmentCapable`, `ServletContextAware`

public class RequestContextFilter extends OncePerRequestFilter
Servlet Filter that exposes the request to the current thread, through both `LocaleContextHolder` and `RequestContextHolder`. To be registered as filter in `web.xml`.

Alternatively, Spring's `RequestContextListener` and Spring's `org.springframework.web.servlet.DispatcherServlet` also expose the same request context to the current thread.

This filter is mainly for use with third-party servlets, for example, the JSF FacesServlet. Within Spring's own web support, DispatcherServlet's processing is perfectly sufficient.

Since:
  2.0
Author:
  Juergen Hoeller, Rod Johnson, Rossen Stoyanchev
See Also:
  - `LocaleContextHolder`
  - `RequestContextHolder`
  - `RequestContextListener`
  - `org.springframework.web.servlet.DispatcherServlet`

## Field Summary

### Fields inherited from class org.springframework.web.filter.OncePerRequestFilter

`ALREADY_FILTERED_SUFFIX`

### Fields inherited from class org.springframework.web.filter.GenericFilterBean

`logger`

## Constructor Summary

Constructors
Constructor
Description
`RequestContextFilter()`

## Method Summary

Modifier and Type
Method
Description
`protected void`
`doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)`
Same contract as for `doFilter`, but guaranteed to be just invoked once per request within a single request thread.
`void`
`setThreadContextInheritable(boolean threadContextInheritable)`
Set whether to expose the LocaleContext and RequestAttributes as inheritable for child threads (using an `InheritableThreadLocal`).
`protected boolean`
`shouldNotFilterAsyncDispatch()`
Returns "false" so that the filter may set up the request context in each asynchronously dispatched thread.
`protected boolean`
`shouldNotFilterErrorDispatch()`
Returns "false" so that the filter may set up the request context in an error dispatch.

### Methods inherited from class org.springframework.web.filter.OncePerRequestFilter

`doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter`

### Methods inherited from class org.springframework.web.filter.GenericFilterBean

`addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### RequestContextFilter

public RequestContextFilter()

## Method Details

### setThreadContextInheritable

public void setThreadContextInheritable(boolean threadContextInheritable)
Set whether to expose the LocaleContext and RequestAttributes as inheritable for child threads (using an `InheritableThreadLocal`).

Default is "false", to avoid side effects on spawned background threads. Switch this to "true" to enable inheritance for custom child threads which are spawned during request processing and only used for this request (that is, ending after their initial task, without reuse of the thread).

WARNING: Do not use inheritance for child threads if you are accessing a thread pool which is configured to potentially add new threads on demand (for example, a JDK `ThreadPoolExecutor`), since this will expose the inherited context to such a pooled thread.

### shouldNotFilterAsyncDispatch

protected boolean shouldNotFilterAsyncDispatch()
Returns "false" so that the filter may set up the request context in each asynchronously dispatched thread.

Overrides:
  `shouldNotFilterAsyncDispatch` in class `OncePerRequestFilter`

### shouldNotFilterErrorDispatch

protected boolean shouldNotFilterErrorDispatch()
Returns "false" so that the filter may set up the request context in an error dispatch.

Overrides:
  `shouldNotFilterErrorDispatch` in class `OncePerRequestFilter`

### doFilterInternal

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
Description copied from class: `OncePerRequestFilter`
Same contract as for `doFilter`, but guaranteed to be just invoked once per request within a single request thread. See `OncePerRequestFilter.shouldNotFilterAsyncDispatch()` for details.

Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.

Specified by:
  `doFilterInternal` in class `OncePerRequestFilter`
Throws:
  `ServletException`
  `IOException`