public class RequestContextFilter extends OncePerRequestFilter
LocaleContextHolder
and
RequestContextHolder
. To be registered as filter in web.xml
.
Alternatively, Spring's RequestContextListener
and Spring's DispatcherServlet
also expose
the same request context to the current thread.
This filter is mainly for use with third-party servlets, e.g. the JSF FacesServlet. Within Spring's own web support, DispatcherServlet's processing is perfectly sufficient.
LocaleContextHolder
,
RequestContextHolder
,
RequestContextListener
,
DispatcherServlet
Modifier and Type | Field and Description |
---|---|
private boolean |
threadContextInheritable |
ALREADY_FILTERED_SUFFIX
logger
Constructor and Description |
---|
RequestContextFilter() |
Modifier and Type | Method and Description |
---|---|
protected void |
doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
Same contract as for
doFilter , but guaranteed to be
just invoked once per request or once per request thread. |
private void |
initContextHolders(HttpServletRequest request,
ServletRequestAttributes requestAttributes) |
private void |
resetContextHolders() |
void |
setThreadContextInheritable(boolean threadContextInheritable)
Set whether to expose the LocaleContext and RequestAttributes as inheritable
for child threads (using an
InheritableThreadLocal ). |
protected boolean |
shouldFilterAsyncDispatches()
The default value is "true" in which case the filter will set up the request
context in each asynchronously dispatched thread.
|
doFilter, getAlreadyFilteredAttributeName, isAsyncDispatch, isLastRequestThread, shouldNotFilter
addRequiredProperty, afterPropertiesSet, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
public void setThreadContextInheritable(boolean threadContextInheritable)
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 (e.g. a JDK ThreadPoolExecutor
),
since this will expose the inherited context to such a pooled thread.
protected boolean shouldFilterAsyncDispatches()
shouldFilterAsyncDispatches
in class OncePerRequestFilter
WebAsyncManager
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, java.io.IOException
OncePerRequestFilter
doFilter
, but guaranteed to be
just invoked once per request or once per request thread.
See OncePerRequestFilter.shouldFilterAsyncDispatches()
for details.
Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
doFilterInternal
in class OncePerRequestFilter
ServletException
java.io.IOException
private void initContextHolders(HttpServletRequest request, ServletRequestAttributes requestAttributes)
private void resetContextHolders()