|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavax.servlet.GenericServlet
javax.servlet.http.HttpServlet
org.springframework.web.servlet.HttpServletBean
org.springframework.web.servlet.FrameworkServlet
public abstract class FrameworkServlet
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:
WebApplicationContext
instance per servlet. The servlet's configuration is determined by beans
in the servlet's namespace.
Subclasses must implement doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
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.
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.
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
setContextClass(java.lang.Class)
,
setContextConfigLocation(java.lang.String)
,
setNamespace(java.lang.String)
,
Serialized FormNested Class Summary | |
---|---|
private class |
FrameworkServlet.ContextRefreshListener
ApplicationListener endpoint that receives events from this servlet's WebApplicationContext only, delegating to onApplicationEvent on the FrameworkServlet instance. |
Field Summary | |
---|---|
private java.lang.String |
contextAttribute
ServletContext attribute to find the WebApplicationContext in |
private java.lang.Class |
contextClass
WebApplicationContext implementation class to create |
private java.lang.String |
contextConfigLocation
Explicit context config location |
static java.lang.Class |
DEFAULT_CONTEXT_CLASS
Default context class for FrameworkServlet. |
static java.lang.String |
DEFAULT_NAMESPACE_SUFFIX
Suffix for WebApplicationContext namespaces. |
private boolean |
dispatchOptionsRequest
Should we dispatch an HTTP OPTIONS request to doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) ? |
private boolean |
dispatchTraceRequest
Should we dispatch an HTTP TRACE request to doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) ? |
private java.lang.String |
namespace
Namespace for this servlet |
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 boolean |
refreshEventReceived
Flag used to detect whether onRefresh has already been called |
static java.lang.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 |
Fields inherited from class org.springframework.web.servlet.HttpServletBean |
---|
logger |
Constructor Summary | |
---|---|
FrameworkServlet()
|
Method Summary | |
---|---|
protected LocaleContext |
buildLocaleContext(javax.servlet.http.HttpServletRequest request)
Build a LocaleContext for the given request, exposing the request's primary locale as current locale. |
protected WebApplicationContext |
createWebApplicationContext(ApplicationContext parent)
Instantiate the WebApplicationContext for this servlet, either a default XmlWebApplicationContext
or a custom context class , if set. |
protected WebApplicationContext |
createWebApplicationContext(WebApplicationContext parent)
Instantiate the WebApplicationContext for this servlet, either a default XmlWebApplicationContext
or a custom context class , if set. |
void |
destroy()
Close the WebApplicationContext of this servlet. |
protected void |
doDelete(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Delegate DELETE requests to processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) . |
protected void |
doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Delegate GET requests to processRequest/doService. |
protected void |
doOptions(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Delegate OPTIONS requests to processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) , if desired. |
protected void |
doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Delegate POST requests to processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) . |
protected void |
doPut(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Delegate PUT requests to processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) . |
protected abstract void |
doService(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse 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(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Delegate TRACE requests to processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) , if desired. |
protected WebApplicationContext |
findWebApplicationContext()
Retrieve a WebApplicationContext from the ServletContext
attribute with the configured name . |
java.lang.String |
getContextAttribute()
Return the name of the ServletContext attribute which should be used to retrieve the WebApplicationContext that this servlet is supposed to use. |
java.lang.Class |
getContextClass()
Return the custom context class. |
java.lang.String |
getContextConfigLocation()
Return the explicit context config location, if any. |
java.lang.String |
getNamespace()
Return the namespace for this servlet, falling back to default scheme if no custom namespace was set: e.g. |
java.lang.String |
getServletContextAttributeName()
Return the ServletContext attribute name for this servlet's WebApplicationContext. |
protected java.lang.String |
getUsernameForRequest(javax.servlet.http.HttpServletRequest request)
Determine the username for the given request. |
WebApplicationContext |
getWebApplicationContext()
Return this servlet's WebApplicationContext. |
protected void |
initFrameworkServlet()
This method will be invoked after any bean properties have been set and the WebApplicationContext has been loaded. |
protected void |
initServletBean()
Overridden method of HttpServletBean , invoked after any bean properties
have been set. |
protected WebApplicationContext |
initWebApplicationContext()
Initialize and publish the WebApplicationContext for this servlet. |
void |
onApplicationEvent(ContextRefreshedEvent event)
Callback that receives refresh events from this servlet's WebApplicationContext. |
protected void |
onRefresh(ApplicationContext context)
Template method which can be overridden to add servlet-specific refresh work. |
protected void |
postProcessWebApplicationContext(ConfigurableWebApplicationContext wac)
Post-process the given WebApplicationContext before it is refreshed and activated as context for this servlet. |
protected void |
processRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Process this request, publishing an event regardless of the outcome. |
void |
refresh()
Refresh this servlet's application context, as well as the dependent state of the servlet. |
void |
setContextAttribute(java.lang.String contextAttribute)
Set the name of the ServletContext attribute which should be used to retrieve the WebApplicationContext that this servlet is supposed to use. |
void |
setContextClass(java.lang.Class contextClass)
Set a custom context class. |
void |
setContextConfigLocation(java.lang.String contextConfigLocation)
Set the context config location explicitly, instead of relying on the default location built from the namespace. |
void |
setDispatchOptionsRequest(boolean dispatchOptionsRequest)
Set whether this servlet should dispatch an HTTP OPTIONS request to the doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) method. |
void |
setDispatchTraceRequest(boolean dispatchTraceRequest)
Set whether this servlet should dispatch an HTTP TRACE request to the doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) method. |
void |
setNamespace(java.lang.String namespace)
Set a custom namespace for this servlet, to be used for building a default context config location. |
void |
setPublishContext(boolean publishContext)
Set whether to publish this servlet's context as a ServletContext attribute, available to all objects in the web container. |
void |
setPublishEvents(boolean publishEvents)
Set whether this servlet should publish a ServletRequestHandledEvent at the end of each request. |
void |
setThreadContextInheritable(boolean threadContextInheritable)
Set whether to expose the LocaleContext and RequestAttributes as inheritable for child threads (using an InheritableThreadLocal ). |
Methods inherited from class org.springframework.web.servlet.HttpServletBean |
---|
addRequiredProperty, getServletContext, getServletName, init, initBeanWrapper |
Methods inherited from class javax.servlet.http.HttpServlet |
---|
doHead, getLastModified, service, service |
Methods inherited from class javax.servlet.GenericServlet |
---|
getInitParameter, getInitParameterNames, getServletConfig, getServletInfo, init, log, log |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String DEFAULT_NAMESPACE_SUFFIX
public static final java.lang.Class DEFAULT_CONTEXT_CLASS
XmlWebApplicationContext
public static final java.lang.String SERVLET_CONTEXT_PREFIX
private java.lang.String contextAttribute
private java.lang.Class contextClass
private java.lang.String namespace
private java.lang.String contextConfigLocation
private boolean publishContext
private boolean publishEvents
private boolean threadContextInheritable
private boolean dispatchOptionsRequest
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
?
private boolean dispatchTraceRequest
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
?
private WebApplicationContext webApplicationContext
private boolean refreshEventReceived
Constructor Detail |
---|
public FrameworkServlet()
Method Detail |
---|
public void setContextAttribute(java.lang.String contextAttribute)
WebApplicationContext
that this servlet is supposed to use.
public java.lang.String getContextAttribute()
WebApplicationContext
that this servlet is supposed to use.
public void setContextClass(java.lang.Class contextClass)
WebApplicationContext
.
When using the default FrameworkServlet implementation,
the context class must also implement the
ConfigurableWebApplicationContext
interface.
createWebApplicationContext(org.springframework.context.ApplicationContext)
public java.lang.Class getContextClass()
public void setNamespace(java.lang.String namespace)
public java.lang.String getNamespace()
public void setContextConfigLocation(java.lang.String contextConfigLocation)
public java.lang.String getContextConfigLocation()
public void setPublishContext(boolean publishContext)
This is especially handy during testing, although it is debatable whether it's good practice to let other application objects access the context this way.
public void setPublishEvents(boolean publishEvents)
ServletRequestHandledEvent
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.
public void setDispatchOptionsRequest(boolean dispatchOptionsRequest)
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
method.
Default is "false", applying HttpServlet
's
default behavior (i.e. enumerating all standard HTTP request methods
as a response to the OPTIONS request).
Turn this flag on if you prefer OPTIONS requests to go through the regular dispatching chain, just like other HTTP requests. This usually means that your controllers will receive those requests; make sure that those endpoints are actually able to handle an OPTIONS request.
Note that HttpServlet's default OPTIONS processing will be applied in any case. Your controllers are simply available to override the default headers and optionally generate a response body.
public void setDispatchTraceRequest(boolean dispatchTraceRequest)
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
method.
Default is "false", applying HttpServlet
's
default behavior (i.e. reflecting the message received back to the client).
Turn this flag on if you prefer TRACE requests to go through the regular dispatching chain, just like other HTTP requests. This usually means that your controllers will receive those requests; make sure that those endpoints are actually able to handle a TRACE request.
Note that HttpServlet's default TRACE processing will be applied
in any case. Your controllers are simply available to override the
default headers and the default body, calling response.reset()
if necessary.
protected final void initServletBean() throws javax.servlet.ServletException
HttpServletBean
, invoked after any bean properties
have been set. Creates this servlet's WebApplicationContext.
initServletBean
in class HttpServletBean
javax.servlet.ServletException
- if subclass initialization failsprotected WebApplicationContext initWebApplicationContext()
Delegates to createWebApplicationContext(org.springframework.context.ApplicationContext)
for actual creation
of the context. Can be overridden in subclasses.
setContextClass(java.lang.Class)
,
setContextConfigLocation(java.lang.String)
protected WebApplicationContext findWebApplicationContext()
WebApplicationContext
from the ServletContext
attribute with the configured name
. The
WebApplicationContext
must have already been loaded and stored in the
ServletContext
before this servlet gets initialized (or invoked).
Subclasses may override this method to provide a different
WebApplicationContext
retrieval strategy.
null
if not foundgetContextAttribute()
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent)
XmlWebApplicationContext
or a custom context class
, if set.
This implementation expects custom contexts to implement the
ConfigurableWebApplicationContext
interface. Can be overridden in subclasses.
Do not forget to register this servlet instance as application listener on the
created context (for triggering its callback
, and to call
ConfigurableApplicationContext.refresh()
before returning the context instance.
parent
- the parent ApplicationContext to use, or null
if none
XmlWebApplicationContext
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
XmlWebApplicationContext
or a custom context class
, if set.
Delegates to #createWebApplicationContext(ApplicationContext).
parent
- the parent WebApplicationContext to use, or null
if none
XmlWebApplicationContext
,
createWebApplicationContext(ApplicationContext)
protected void postProcessWebApplicationContext(ConfigurableWebApplicationContext wac)
The default implementation is empty. refresh()
will
be called automatically after this method returns.
wac
- the configured WebApplicationContext (not refreshed yet)createWebApplicationContext(org.springframework.context.ApplicationContext)
,
ConfigurableApplicationContext.refresh()
public java.lang.String getServletContextAttributeName()
The default implementation returns
SERVLET_CONTEXT_PREFIX + servlet name
.
SERVLET_CONTEXT_PREFIX
,
HttpServletBean.getServletName()
public final WebApplicationContext getWebApplicationContext()
protected void initFrameworkServlet() throws javax.servlet.ServletException
javax.servlet.ServletException
- in case of an initialization exceptionpublic void refresh()
getWebApplicationContext()
,
ConfigurableApplicationContext.refresh()
public void onApplicationEvent(ContextRefreshedEvent event)
The default implementation calls onRefresh(org.springframework.context.ApplicationContext)
,
triggering a refresh of this servlet's context-dependent state.
event
- the incoming ApplicationContext eventprotected void onRefresh(ApplicationContext context)
This implementation is empty.
context
- the current WebApplicationContextrefresh()
protected final void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
Will also be invoked by HttpServlet's default implementation of doHead
,
with a NoBodyResponse
that just captures the content length.
doGet
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
java.io.IOException
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
HttpServlet.doHead(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected final void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
.
doPost
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
java.io.IOException
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected final void doPut(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
.
doPut
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
java.io.IOException
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected final void doDelete(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
.
doDelete
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
java.io.IOException
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected void doOptions(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
, if desired.
Applies HttpServlet's standard OPTIONS processing first.
doOptions
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
java.io.IOException
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected void doTrace(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
, if desired.
Applies HttpServlet's standard TRACE processing first.
doTrace
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
java.io.IOException
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected final void processRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
The actual event handling is performed by the abstract
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
template method.
javax.servlet.ServletException
java.io.IOException
protected LocaleContext buildLocaleContext(javax.servlet.http.HttpServletRequest request)
request
- current HTTP request
protected java.lang.String getUsernameForRequest(javax.servlet.http.HttpServletRequest request)
The default implementation takes the name of the UserPrincipal, if any. Can be overridden in subclasses.
request
- current HTTP request
null
if none foundHttpServletRequest.getUserPrincipal()
protected abstract void doService(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.lang.Exception
The contract is essentially the same as that for the commonly overridden
doGet
or doPost
methods of HttpServlet.
This class intercepts calls to ensure that exception handling and event publication takes place.
request
- current HTTP requestresponse
- current HTTP response
java.lang.Exception
- in case of any kind of processing failureHttpServlet.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
HttpServlet.doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
public void destroy()
destroy
in interface javax.servlet.Servlet
destroy
in class javax.servlet.GenericServlet
ConfigurableApplicationContext.close()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |