org.springframework.web.servlet
Class FrameworkServlet

java.lang.Object
  extended byjavax.servlet.GenericServlet
      extended byjavax.servlet.http.HttpServlet
          extended byorg.springframework.web.servlet.HttpServletBean
              extended byorg.springframework.web.servlet.FrameworkServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
DispatcherServlet

public abstract 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:

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.

Version:
$Revision: 1.14 $
Author:
Rod Johnson
See Also:
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse), initFrameworkServlet(), setContextClass(java.lang.Class), setContextConfigLocation(java.lang.String), setNamespace(java.lang.String), Serialized Form

Field Summary
static java.lang.Class DEFAULT_CONTEXT_CLASS
          Default context class for FrameworkServlet.
static java.lang.String DEFAULT_NAMESPACE_SUFFIX
          Suffix for namespace bean factory names.
static java.lang.String SERVLET_CONTEXT_PREFIX
          Prefix for the ServletContext attribute for the web application context.
 
Fields inherited from class org.springframework.web.servlet.HttpServletBean
logger
 
Constructor Summary
FrameworkServlet()
           
 
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()
           
protected  void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          It's up to each subclass to decide whether or not it supports a request method.
protected  void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          It's up to each subclass to decide whether or not it supports a request method.
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.
 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.
 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.
 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 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.
 
Methods inherited from class org.springframework.web.servlet.HttpServletBean
init, setRequiredProperty
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_NAMESPACE_SUFFIX

public static final java.lang.String DEFAULT_NAMESPACE_SUFFIX
Suffix for namespace bean factory names. 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'.

See Also:
Constant Field Values

DEFAULT_CONTEXT_CLASS

public static final java.lang.Class DEFAULT_CONTEXT_CLASS
Default context class for FrameworkServlet.

See Also:
XmlWebApplicationContext

SERVLET_CONTEXT_PREFIX

public static final java.lang.String SERVLET_CONTEXT_PREFIX
Prefix for the ServletContext attribute for the web application context. The completion is the servlet name.

Constructor Detail

FrameworkServlet

public FrameworkServlet()
Method Detail

setContextClass

public final void setContextClass(java.lang.Class contextClass)
Set a custom context class. This class must be of type WebApplicationContext, with the default FrameworkServlet implementing ConfigurableWebApplicationContext.

See Also:
createWebApplicationContext(org.springframework.web.context.WebApplicationContext)

getContextClass

public java.lang.Class getContextClass()
Return the custom context class.


setNamespace

public void setNamespace(java.lang.String namespace)
Set a custom namespace for this servlet, to be used for building a default context config location.


getNamespace

public java.lang.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".


setContextConfigLocation

public void setContextConfigLocation(java.lang.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.


getContextConfigLocation

public java.lang.String getContextConfigLocation()
Return the explicit context config location, if any.


setPublishContext

public final void setPublishContext(boolean publishContext)
Set whether to publish this servlet's context as a ServletContext attribute. Default is true.

Parameters:
publishContext - whether we should publish this servlet's WebApplicationContext as a ServletContext attribute, available to all objects in this 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.

isPublishContext

public boolean isPublishContext()
Return whether to publish this servlet's context as a ServletContext attribute.


getWebApplicationContext

public final WebApplicationContext getWebApplicationContext()
Return this servlet's WebApplicationContext.


getServletContextAttributeName

public final java.lang.String getServletContextAttributeName()
Return the ServletContext attribute name for this servlet's WebApplicationContext.


initServletBean

protected final void initServletBean()
                              throws javax.servlet.ServletException,
                                     BeansException
Overridden method of HttpServletBean, invoked after any bean properties have been set. Creates this servlet's WebApplicationContext.

Overrides:
initServletBean in class HttpServletBean
Throws:
javax.servlet.ServletException - if subclass initialization fails
BeansException

initWebApplicationContext

protected WebApplicationContext initWebApplicationContext()
                                                   throws BeansException
Initialize and publish the WebApplicationContext for this servlet. Delegates to createWebApplicationContext for actual creation. Can be overridden in subclasses.

Throws:
BeansException - if the context couldn't be initialized
See Also:
createWebApplicationContext(org.springframework.web.context.WebApplicationContext)

createWebApplicationContext

protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
                                                     throws BeansException
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.

Throws:
BeansException - if the context couldn't be initialized
See Also:
setContextClass(java.lang.Class), XmlWebApplicationContext

destroy

public void destroy()

doGet

protected final void doGet(javax.servlet.http.HttpServletRequest request,
                           javax.servlet.http.HttpServletResponse response)
                    throws javax.servlet.ServletException,
                           java.io.IOException
It's up to each subclass to decide whether or not it supports a request method. It should throw a Servlet exception if it doesn't support a particular request type. This might commonly be done with GET for forms, for example

Throws:
javax.servlet.ServletException
java.io.IOException

doPost

protected final void doPost(javax.servlet.http.HttpServletRequest request,
                            javax.servlet.http.HttpServletResponse response)
                     throws javax.servlet.ServletException,
                            java.io.IOException
It's up to each subclass to decide whether or not it supports a request method. It should throw a Servlet exception if it doesn't support a particular request type. This might commonly be done with GET for forms, for example

Throws:
javax.servlet.ServletException
java.io.IOException

initFrameworkServlet

protected void initFrameworkServlet()
                             throws javax.servlet.ServletException,
                                    BeansException
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.

Throws:
javax.servlet.ServletException - in case of an initialization exception
BeansException - if thrown by ApplicationContext methods

doService

protected abstract void doService(javax.servlet.http.HttpServletRequest request,
                                  javax.servlet.http.HttpServletResponse response)
                           throws java.lang.Exception
Subclasses must implement this method to do the work of request handling. The contract is the same as that for the doGet or doPost methods of HttpServlet. This class intercepts calls to ensure that event publication takes place.

Throws:
java.lang.Exception - in case of any kind of processing failure
See Also:
HttpServlet.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse), HttpServlet.doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)


Copyright (C) 2003-2004 The Spring Framework Project.