Top Description Inners Fields Constructors Methods
org.springframework.web.servlet

public abstract Class FrameworkServlet

extends HttpServletBean
implements ApplicationContextAware
Class Inheritance
All Implemented Interfaces
org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.Aware
Known Direct Subclasses
org.springframework.web.servlet.DispatcherServlet
Annotations
@SuppressWarnings:serial
Imports
java.io.IOException, java.security.Principal, java.util.ArrayList, .Collection, .List, .Set, java.util.concurrent.Callable, java.util.stream.Collectors, jakarta.servlet.DispatcherType, .ServletContext, .ServletException, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, .HttpServletResponseWrapper, org.springframework.beans.BeanUtils, org.springframework.context.ApplicationContext, .ApplicationContextAware, .ApplicationContextException, .ApplicationContextInitializer, .ApplicationListener, .ConfigurableApplicationContext, org.springframework.context.event.ContextRefreshedEvent, .SourceFilteringListener, org.springframework.context.i18n.LocaleContext, .LocaleContextHolder, .SimpleLocaleContext, org.springframework.core.GenericTypeResolver, org.springframework.core.annotation.AnnotationAwareOrderComparator, org.springframework.core.env.ConfigurableEnvironment, org.springframework.http.HttpHeaders, .HttpMethod, .HttpStatusCode, org.springframework.lang.Nullable, org.springframework.util.ClassUtils, .ObjectUtils, .StringUtils, org.springframework.web.context.ConfigurableWebApplicationContext, .ConfigurableWebEnvironment, .ContextLoader, .WebApplicationContext, org.springframework.web.context.request.NativeWebRequest, .RequestAttributes, .RequestContextHolder, .ServletRequestAttributes, org.springframework.web.context.request.async.CallableProcessingInterceptor, .WebAsyncManager, .WebAsyncUtils, org.springframework.web.context.support.ServletRequestHandledEvent, .WebApplicationContextUtils, .XmlWebApplicationContext, org.springframework.web.cors.CorsUtils, org.springframework.web.util.WebUtils

Base servlet for Spring's web framework. Provides integration with a Spring application context, in a JavaBean-based overall solution.

This class offers the following functionality:

Subclasses must implement doService to handle requests. Because this extends HttpServletBean rather than HttpServlet directly, bean properties are automatically mapped onto it. Subclasses can override initFrameworkServlet() for custom initialization.

Detects a "contextClass" parameter at the servlet init-param level, falling back to the default context class, XmlWebApplicationContext, if not found. Note that, with the default FrameworkServlet, a custom context class needs to implement the ConfigurableWebApplicationContext SPI.

Accepts an optional "contextInitializerClasses" servlet init-param that specifies one or more ApplicationContextInitializer classes. The managed web application context will be delegated to these initializers, allowing for additional programmatic configuration, e.g. adding property sources or activating profiles against the context's environment. See also org.springframework.web.context.ContextLoader which supports a "contextInitializerClasses" context-param with identical semantics for the "root" web application context.

Passes a "contextConfigLocation" servlet init-param to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, like "test-servlet.xml, myServlet.xml". If not explicitly specified, the context implementation is supposed to build a default location from the namespace of the servlet.

Note

In case of multiple config locations, later bean definitions will override ones defined in earlier loaded files, at least when using Spring's default ApplicationContext implementation. This can be leveraged to deliberately override certain bean definitions via an extra XML file.

The default namespace is "'servlet-name'-servlet", e.g. "test-servlet" for a servlet-name "test" (leading to a "/WEB-INF/test-servlet.xml" default location with XmlWebApplicationContext). The namespace can also be set explicitly via the "namespace" servlet init-param.

FrameworkServlet may be injected with a web application context, rather than creating its own internally. This is useful in Servlet 3.0+ environments, which support programmatic registration of servlet instances. See FrameworkServlet(WebApplicationContext) Javadoc for details.

Authors
Rod Johnson, Juergen Hoeller, Sam Brannen, Chris Beams, Rossen Stoyanchev, Phillip Webb
See Also
doService, setContextClass, setContextConfigLocation, setContextInitializerClasses, setNamespace

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
FrameworkServlet.ContextRefreshListener

ApplicationListener endpoint that receives events from this servlet's WebApplicationContext only, delegating to onApplicationEvent on the FrameworkServlet instance.

private class
FrameworkServlet.RequestBindingInterceptor

CallableProcessingInterceptor implementation that initializes and resets FrameworkServlet's context holders, i.e. LocaleContextHolder and RequestContextHolder.

Field Summary

Modifier and TypeField and Description
private String
contextAttribute

ServletContext attribute to find the WebApplicationContext in.

private Class<?>
contextClass

WebApplicationContext implementation class to create.

private String
contextConfigLocation

Explicit context config location.

private String
contextId

WebApplicationContext id to assign.

private String
contextInitializerClasses

Comma-delimited ApplicationContextInitializer class names set through init param.

private final List<ApplicationContextInitializer<ConfigurableApplicationContext>>
contextInitializers

Actual ApplicationContextInitializer instances to apply to the context.

public static final Class<?>
DEFAULT_CONTEXT_CLASS

Default context class for FrameworkServlet.

public static final String
DEFAULT_NAMESPACE_SUFFIX

Suffix for WebApplicationContext namespaces.

private boolean
dispatchOptionsRequest

Should we dispatch an HTTP OPTIONS request to doService?.

private boolean
dispatchTraceRequest

Should we dispatch an HTTP TRACE request to doService?.

private boolean
enableLoggingRequestDetails

Whether to log potentially sensitive info (request params at DEBUG + headers at TRACE).

private static final Set<String>
private static final String
INIT_PARAM_DELIMITERS

Any number of these characters are considered delimiters between multiple values in a single init-param String value.

private String
namespace

Namespace for this servlet.

private final Object
onRefreshMonitor

Monitor for synchronized onRefresh execution.

private boolean
publishContext

Should we publish the context as a ServletContext attribute?.

private boolean
publishEvents

Should we publish a ServletRequestHandledEvent at the end of each request?.

private volatile boolean
refreshEventReceived

Flag used to detect whether onRefresh has already been called.

public static final String
SERVLET_CONTEXT_PREFIX

Prefix for the ServletContext attribute for the WebApplicationContext.

private boolean
threadContextInheritable

Expose LocaleContext and RequestAttributes as inheritable for child threads?.

private WebApplicationContext
webApplicationContext

WebApplicationContext for this servlet.

private boolean
webApplicationContextInjected

If the WebApplicationContext was injected via setApplicationContext.

Inherited from org.springframework.web.servlet.HttpServletBean:
logger

Constructor Summary

AccessConstructor and Description
public
FrameworkServlet()

Create a new FrameworkServlet that will create its own internal web application context based on defaults and values provided through servlet init-params.

public
FrameworkServlet(WebApplicationContext
the context to use
webApplicationContext
)

Create a new FrameworkServlet with the given web application context.

Method Summary

Modifier and TypeMethod and Description
protected void
applyInitializers(ConfigurableApplicationContext
the configured WebApplicationContext (not refreshed yet)
wac
)

Delegate the WebApplicationContext before it is refreshed to any ApplicationContextInitializer instances specified by the "contextInitializerClasses" servlet init-param.

protected LocaleContext

Returns:

the corresponding LocaleContext, or null if none to bind
buildLocaleContext
(HttpServletRequest
current HTTP request
request
)

Build a LocaleContext for the given request, exposing the request's primary locale as current locale.

protected ServletRequestAttributes

Returns:

the ServletRequestAttributes to bind, or null to preserve the previously bound instance (or not binding any, if none bound before)
buildRequestAttributes
(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
RequestAttributes
pre-bound RequestAttributes instance, if any
previousAttributes
)

Build ServletRequestAttributes for the given request (potentially also holding a reference to the response), taking pre-bound attributes (and their type) into consideration.

protected void
protected WebApplicationContext

Returns:

the WebApplicationContext for this servlet
createWebApplicationContext
(ApplicationContext
the parent ApplicationContext to use, or null if none
parent
)

Instantiate the WebApplicationContext for this servlet, either a default org.springframework.web.context.support.XmlWebApplicationContext or a custom context class, if set.

protected WebApplicationContext

Returns:

the WebApplicationContext for this servlet
createWebApplicationContext
(WebApplicationContext
the parent WebApplicationContext to use, or null if none
parent
)

Instantiate the WebApplicationContext for this servlet, either a default org.springframework.web.context.support.XmlWebApplicationContext or a custom context class, if set.

public void
destroy()

Overrides jakarta.servlet.GenericServlet.destroy.

Implements jakarta.servlet.Servlet.destroy.

Close the WebApplicationContext of this servlet.

protected final void
doDelete(HttpServletRequest
the HttpServletRequest object that contains the request the client made of the servlet
request
,
HttpServletResponse
the HttpServletResponse object that contains the response the servlet returns to the client
response
)

Overrides jakarta.servlet.http.HttpServlet.doDelete.

Delegate DELETE requests to processRequest.

protected final void
doGet(HttpServletRequest
an HttpServletRequest object that contains the request the client has made of the servlet
request
,
HttpServletResponse
an HttpServletResponse object that contains the response the servlet sends to the client
response
)

Overrides jakarta.servlet.http.HttpServlet.doGet.

Delegate GET requests to processRequest/doService.

protected void
doOptions(HttpServletRequest
the HttpServletRequest object that contains the request the client made of the servlet
request
,
HttpServletResponse
the HttpServletResponse object that contains the response the servlet returns to the client
response
)

Overrides jakarta.servlet.http.HttpServlet.doOptions.

Delegate OPTIONS requests to processRequest, if desired.

protected final void
doPost(HttpServletRequest
an HttpServletRequest object that contains the request the client has made of the servlet
request
,
HttpServletResponse
an HttpServletResponse object that contains the response the servlet sends to the client
response
)

Overrides jakarta.servlet.http.HttpServlet.doPost.

Delegate POST requests to processRequest.

protected final void
doPut(HttpServletRequest
the HttpServletRequest object that contains the request the client made of the servlet
request
,
HttpServletResponse
the HttpServletResponse object that contains the response the servlet returns to the client
response
)

Overrides jakarta.servlet.http.HttpServlet.doPut.

Delegate PUT requests to processRequest.

protected abstract void
doService(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
)

Subclasses must implement this method to do the work of request handling, receiving a centralized callback for GET, POST, PUT and DELETE.

protected void
doTrace(HttpServletRequest
the HttpServletRequest object that contains the request the client made of the servlet
request
,
HttpServletResponse
the HttpServletResponse object that contains the response the servlet returns to the client
response
)

Overrides jakarta.servlet.http.HttpServlet.doTrace.

Delegate TRACE requests to processRequest, if desired.

protected WebApplicationContext

Returns:

the WebApplicationContext for this servlet, or null if not found
findWebApplicationContext
()

Retrieve a WebApplicationContext from the ServletContext attribute with the configured name.

public String
getContextAttribute()

Return the name of the ServletContext attribute which should be used to retrieve the WebApplicationContext that this servlet is supposed to use.

public Class<?>
getContextClass()

Return the custom context class.

public String
getContextConfigLocation()

Return the explicit context config location, if any.

public String
getContextId()

Return the custom WebApplicationContext id, if any.

public String
getNamespace()

Return the namespace for this servlet, falling back to default scheme if no custom namespace was set: e.g. "test-servlet" for a servlet named "test".

public String
getServletContextAttributeName()

Return the ServletContext attribute name for this servlet's WebApplicationContext.

protected String

Returns:

the username, or null if none found
getUsernameForRequest
(HttpServletRequest
current HTTP request
request
)

Determine the username for the given request.

public final WebApplicationContext
getWebApplicationContext()

Return this servlet's WebApplicationContext.

private void
initContextHolders(HttpServletRequest request, LocaleContext localeContext, RequestAttributes requestAttributes)

protected void
initFrameworkServlet()

This method will be invoked after any bean properties have been set and the WebApplicationContext has been loaded.

protected final void
initServletBean()

Overrides org.springframework.web.servlet.HttpServletBean.initServletBean.

Overridden method of HttpServletBean, invoked after any bean properties have been set.

protected WebApplicationContext

Returns:

the WebApplicationContext instance
initWebApplicationContext
()

Initialize and publish the WebApplicationContext for this servlet.

public boolean
isEnableLoggingRequestDetails()

Whether logging of potentially sensitive, request details at DEBUG and TRACE level is allowed.

private ApplicationContextInitializer<ConfigurableApplicationContext>
private void
logResult(HttpServletRequest request, HttpServletResponse response, Throwable failureCause, WebAsyncManager asyncManager)

public void
onApplicationEvent(ContextRefreshedEvent
the incoming ApplicationContext event
event
)

Callback that receives refresh events from this servlet's WebApplicationContext.

protected void
onRefresh(ApplicationContext
the current WebApplicationContext
context
)

Template method which can be overridden to add servlet-specific refresh work.

protected void
postProcessWebApplicationContext(ConfigurableWebApplicationContext
the configured WebApplicationContext (not refreshed yet)
wac
)

Post-process the given WebApplicationContext before it is refreshed and activated as context for this servlet.

protected final void
processRequest(HttpServletRequest request, HttpServletResponse response)

Process this request, publishing an event regardless of the outcome.

private void
publishRequestHandledEvent(HttpServletRequest request, HttpServletResponse response, long startTime, Throwable failureCause)

public void
refresh()

Refresh this servlet's application context, as well as the dependent state of the servlet.

private void
resetContextHolders(HttpServletRequest request, LocaleContext prevLocaleContext, RequestAttributes previousAttributes)

protected void
service(HttpServletRequest
the HttpServletRequest object that contains the request the client made of the servlet
request
,
HttpServletResponse
the HttpServletResponse object that contains the response the servlet returns to the client
response
)

Overrides jakarta.servlet.http.HttpServlet.service.

Override the parent class implementation in order to intercept requests using PATCH or non-standard HTTP methods (WebDAV).

public void
setApplicationContext(ApplicationContext
the ApplicationContext object to be used by this object
applicationContext
)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Called by Spring via ApplicationContextAware to inject the current application context.

public void
setContextAttribute(String contextAttribute)

Set the name of the ServletContext attribute which should be used to retrieve the WebApplicationContext that this servlet is supposed to use.

public void
setContextClass(Class<?> contextClass)

Set a custom context class.

public void
setContextConfigLocation(String contextConfigLocation)

Set the context config location explicitly, instead of relying on the default location built from the namespace.

public void
setContextId(String contextId)

Specify a custom WebApplicationContext id, to be used as serialization id for the underlying BeanFactory.

public void
setContextInitializerClasses(String contextInitializerClasses)

Specify the set of fully-qualified ApplicationContextInitializer class names, per the optional "contextInitializerClasses" servlet init-param.

public void
setContextInitializers(ApplicationContextInitializer<?>... initializers)

Specify which ApplicationContextInitializer instances should be used to initialize the application context used by this FrameworkServlet.

public void
setDispatchOptionsRequest(boolean dispatchOptionsRequest)

Set whether this servlet should dispatch an HTTP OPTIONS request to the doService method.

public void
setDispatchTraceRequest(boolean dispatchTraceRequest)

Set whether this servlet should dispatch an HTTP TRACE request to the doService method.

public void
setEnableLoggingRequestDetails(boolean
whether to enable or not
enable
)

Whether to log request params at DEBUG level, and headers at TRACE level.

public void
setNamespace(String namespace)

Set a custom namespace for this servlet, to be used for building a default context config location.

public void
setPublishContext(boolean publishContext)

Set whether to publish this servlet's context as a ServletContext attribute, available to all objects in the web container.

public void
setPublishEvents(boolean publishEvents)

Set whether this servlet should publish a ServletRequestHandledEvent at the end of each request.

public void
setThreadContextInheritable(boolean threadContextInheritable)

Set whether to expose the LocaleContext and RequestAttributes as inheritable for child threads (using an java.lang.InheritableThreadLocal).

Inherited from org.springframework.web.servlet.HttpServletBean:
addRequiredPropertycreateEnvironmentgetEnvironmentgetServletNameinitinitBeanWrappersetEnvironment