org.springframework.web.servlet
Class FrameworkServlet

public class FrameworkServlet
extends HttpServletBean
Base servlet for servlets within the web framework. Allows integration with an application context, in a JavaBean-based overall solution.

This class offers the following functionality:

  • Uses a WebApplicationContext to access a BeanFactory. The servlet's configuration is determined by beans in the servlet's namespace.
  • Publishes events on request processing, whether or not a request is successfully handled.

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

Regards a "contextClass" parameter at the servlet init-param level, falling back to the default context class (XmlWebApplicationContext) if not found. With the default FrameworkServlet, a context class needs to implement ConfigurableWebApplicationContext.

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 one of Spring's default ApplicationContext implementations. 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.

SinceNot specified.
VersionNot specified.
AuthorRod Johnson, Juergen Hoeller
Wiki javadoc Use textile entry format.
Add your comments here.
Field Summary
static Class DEFAULT_CONTEXT_CLASS
Default context class for FrameworkServlet.
static String DEFAULT_NAMESPACE_SUFFIX
Suffix for WebApplicationContext namespaces.
static String SERVLET_CONTEXT_PREFIX
Prefix for the ServletContext attribute for the WebApplicationContext.
Fields inherited from org.springframework.web.servletHttpServletBean
Constructor Summary
FrameworkServlet()
No description provided.
Method Summary
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( HttpServletRequest request, HttpServletResponse response )
Delegate DELETE requests to processRequest/doService.
protected void doGet( HttpServletRequest request, HttpServletResponse response )
Delegate GET requests to processRequest/doService.
protected void doPost( HttpServletRequest request, HttpServletResponse response )
Delegate POST requests to processRequest/doService.
protected void doPut( HttpServletRequest request, HttpServletResponse response )
Delegate PUT requests to processRequest/doService.
protected void doService( HttpServletRequest request, HttpServletResponse response )
Subclasses must implement this method to do the work of request handling, receiving a centralized callback for GET, POST, PUT and DELETE.
Class getContextClass()
Return the custom context class.
String getContextConfigLocation()
Return the explicit context config location, if any.
String getNamespace()
Return the namespace for this servlet, falling back to default scheme if no custom namespace was set: e.g.
String getServletContextAttributeName()
Return the ServletContext attribute name for this servlet's WebApplicationContext.
protected String getUsernameForRequest( 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.
boolean isPublishContext()
Return whether to publish this servlet's context as a ServletContext attribute.
boolean isPublishEvents()
Return whether this servlet should publish a RequestHandlerEvent at the end of each request.
protected void processRequest( HttpServletRequest request, HttpServletResponse response )
Process this request, publishing an event regardless of the outcome.
void setContextClass( Class contextClass )
Set a custom context class.
void setContextConfigLocation( String contextConfigLocation )
Set the context config location explicitly, instead of relying on the default location built from the namespace.
void setNamespace( 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 RequestHandlerEvent at the end of each request.
Methods inherited from org.springframework.web.servletHttpServletBean
DEFAULT_CONTEXT_CLASS
public static Class DEFAULT_CONTEXT_CLASS
Default context class for FrameworkServlet.
Wiki javadoc Use textile entry format.
Add your comments here.
DEFAULT_NAMESPACE_SUFFIX
public static String DEFAULT_NAMESPACE_SUFFIX
Suffix for WebApplicationContext namespaces. If a servlet of this class is given the name "test" in a context, the namespace used by the servlet will resolve to "test-servlet".
Wiki javadoc Use textile entry format.
Add your comments here.
SERVLET_CONTEXT_PREFIX
public static String SERVLET_CONTEXT_PREFIX
Prefix for the ServletContext attribute for the WebApplicationContext. The completion is the servlet name.
Wiki javadoc Use textile entry format.
Add your comments here.
FrameworkServlet
public FrameworkServlet ( )
No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
createWebApplicationContext
protected WebApplicationContext createWebApplicationContext ( WebApplicationContext parent )
Instantiate the WebApplicationContext for this servlet, either a default XmlWebApplicationContext or a custom context class if set. This implementation expects custom contexts to implement ConfigurableWebApplicationContext. Can be overridden in subclasses.
Parameters
TypeNameDescription
WebApplicationContext parent the parent ApplicationContext to use, or null if none
Exceptions
BeansException if the context couldn't be initialized
Wiki javadoc Use textile entry format.
Add your comments here.
destroy
public void destroy ( )
Close the WebApplicationContext of this servlet.
Overrides method in GenericServlet
Returns void No description provided.
See also
Wiki javadoc Use textile entry format.
Add your comments here.
doDelete
protected void doDelete ( HttpServletRequest request, HttpServletResponse response )
Delegate DELETE requests to processRequest/doService.
Overrides method in HttpServlet
Parameters
TypeNameDescription
HttpServletRequest request No description provided.
HttpServletResponse response No description provided.
Returns void No description provided.
Exceptions
ServletException No description provided.
IOException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
doGet
protected void doGet ( HttpServletRequest request, HttpServletResponse response )
Delegate GET requests to processRequest/doService.

Will also be invoked by HttpServlet's default implementation of doHead, with a NoBodyResponse that just captures the content length.

Overrides method in HttpServlet
Parameters
TypeNameDescription
HttpServletRequest request No description provided.
HttpServletResponse response No description provided.
Returns void No description provided.
Exceptions
ServletException No description provided.
IOException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
doPost
protected void doPost ( HttpServletRequest request, HttpServletResponse response )
Delegate POST requests to processRequest/doService.
Overrides method in HttpServlet
Parameters
TypeNameDescription
HttpServletRequest request No description provided.
HttpServletResponse response No description provided.
Returns void No description provided.
Exceptions
ServletException No description provided.
IOException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
doPut
protected void doPut ( HttpServletRequest request, HttpServletResponse response )
Delegate PUT requests to processRequest/doService.
Overrides method in HttpServlet
Parameters
TypeNameDescription
HttpServletRequest request No description provided.
HttpServletResponse response No description provided.
Returns void No description provided.
Exceptions
ServletException No description provided.
IOException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
doService
protected void doService ( HttpServletRequest request, HttpServletResponse response )
throws
Subclasses must implement this method to do the work of request handling, receiving a centralized callback for GET, POST, PUT and DELETE.

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.

Parameters
TypeNameDescription
HttpServletRequest request current HTTP request
HttpServletResponse response current HTTP response
Returns void No description provided.
Exceptions
Exception in case of any kind of processing failure
Wiki javadoc Use textile entry format.
Add your comments here.
getContextClass
public Class getContextClass ( )
Return the custom context class.
Wiki javadoc Use textile entry format.
Add your comments here.
getContextConfigLocation
public String getContextConfigLocation ( )
Return the explicit context config location, if any.
Wiki javadoc Use textile entry format.
Add your comments here.
getNamespace
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".
Wiki javadoc Use textile entry format.
Add your comments here.
getServletContextAttributeName
public String getServletContextAttributeName ( )
Return the ServletContext attribute name for this servlet's WebApplicationContext. Default implementation returns SERVLET_CONTEXT_PREFIX + servlet name.
Wiki javadoc Use textile entry format.
Add your comments here.
getUsernameForRequest
protected String getUsernameForRequest ( HttpServletRequest request )
Determine the username for the given request. Default implementation takes the name of the UserPrincipal, if any. Can be overridden in subclasses.
Parameters
TypeNameDescription
HttpServletRequest request current HTTP request
Wiki javadoc Use textile entry format.
Add your comments here.
getWebApplicationContext
public WebApplicationContext getWebApplicationContext ( )
Return this servlet's WebApplicationContext.
Wiki javadoc Use textile entry format.
Add your comments here.
initFrameworkServlet
protected void initFrameworkServlet ( )
This method will be invoked after any bean properties have been set and the WebApplicationContext has been loaded. The default implementation is empty; subclasses may override this method to perform any initialization they require.
Returns void No description provided.
Exceptions
ServletException in case of an initialization exception
BeansException if thrown by ApplicationContext methods
Wiki javadoc Use textile entry format.
Add your comments here.
initServletBean
protected void initServletBean ( )
Overridden method of HttpServletBean, invoked after any bean properties have been set. Creates this servlet's WebApplicationContext.
Overrides method in HttpServletBean
Returns void No description provided.
Exceptions
ServletException No description provided.
BeansException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
initWebApplicationContext
protected WebApplicationContext initWebApplicationContext ( )
Initialize and publish the WebApplicationContext for this servlet. Delegates to createWebApplicationContext for actual creation. Can be overridden in subclasses.
Exceptions
BeansException if the context couldn't be initialized
Wiki javadoc Use textile entry format.
Add your comments here.
isPublishContext
public boolean isPublishContext ( )
Return whether to publish this servlet's context as a ServletContext attribute.
Wiki javadoc Use textile entry format.
Add your comments here.
isPublishEvents
public boolean isPublishEvents ( )
Return whether this servlet should publish a RequestHandlerEvent at the end of each request.
Wiki javadoc Use textile entry format.
Add your comments here.
processRequest
protected void processRequest ( HttpServletRequest request, HttpServletResponse response )
Process this request, publishing an event regardless of the outcome. The actually event handling is performed by the abstract doService() template method.
Parameters
TypeNameDescription
HttpServletRequest request No description provided.
HttpServletResponse response No description provided.
Returns void No description provided.
Exceptions
ServletException No description provided.
IOException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setContextClass
public void setContextClass ( Class contextClass )
Set a custom context class. This class must be of type WebApplicationContext; when using the default FrameworkServlet implementation, the context class must also implement ConfigurableWebApplicationContext.
Parameters
TypeNameDescription
Class contextClass No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setContextConfigLocation
public void setContextConfigLocation ( String contextConfigLocation )
Set the context config location explicitly, instead of relying on the default location built from the namespace. This location string can consist of multiple locations separated by any number of commas and spaces.
Parameters
TypeNameDescription
String contextConfigLocation No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setNamespace
public void setNamespace ( String namespace )
Set a custom namespace for this servlet, to be used for building a default context config location.
Parameters
TypeNameDescription
String namespace No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setPublishContext
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. Default is "true".

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.

Parameters
TypeNameDescription
boolean publishContext No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setPublishEvents
public void setPublishEvents ( boolean publishEvents )
Set whether this servlet should publish a RequestHandlerEvent at the end of each request. Default is "true"; can be turned off for a slight performance improvement, provided that no ApplicationListeners rely on such events.
Parameters
TypeNameDescription
boolean publishEvents No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.