org.springframework.web.servlet
Class FrameworkServlet

|
+--org.springframework.web.servlet.HttpServletBean
  |
  +--org.springframework.web.servlet.FrameworkServlet
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.

Author:
Rod Johnson
Version: $Revision: 1.12 $
See Also: doService(HttpServletRequest,HttpServletResponse), initFrameworkServlet(), setContextClass(java.lang.Class), setContextConfigLocation(java.lang.String), setNamespace(java.lang.String)

Field Summary
 final static ClassDEFAULT_CONTEXT_CLASS
          Default context class for FrameworkServlet.
 final static StringDEFAULT_NAMESPACE_SUFFIX
          Suffix for namespace bean factory names.
 final static StringSERVLET_CONTEXT_PREFIX
          Prefix for the ServletContext attribute for the web application context.

Method Summary
 voiddestroy()
           
 ClassgetContextClass()
          Return the custom context class.
 StringgetContextConfigLocation()
          Return the explicit context config location, if any.
 StringgetNamespace()
          Return the namespace for this servlet, falling back to default scheme if no custom namespace was set: e.g.
 final StringgetServletContextAttributeName()
          Return the ServletContext attribute name for this servlet's WebApplicationContext.
 final WebApplicationContextgetWebApplicationContext()
          Return this servlet's WebApplicationContext.
 booleanisPublishContext()
          Return whether to publish this servlet's context as a ServletContext attribute.
 final voidsetContextClass(Class contextClass)
          Set a custom context class.
 voidsetContextConfigLocation(String contextConfigLocation)
          Set the context config location explicitly, instead of relying on the default location built from the namespace.
 voidsetNamespace(String namespace)
          Set a custom namespace for this servlet, to be used for building a default context config location.
 final voidsetPublishContext(boolean publishContext)
          Set whether to publish this servlet's context as a ServletContext attribute.

Methods inherited from class org.springframework.web.servlet.HttpServletBean
init

Field Detail

DEFAULT_CONTEXT_CLASS

public final static Class DEFAULT_CONTEXT_CLASS
Default context class for FrameworkServlet.
See Also:
XmlWebApplicationContext

DEFAULT_NAMESPACE_SUFFIX

public final static 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'.

SERVLET_CONTEXT_PREFIX

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

destroy

public void destroy()

getContextClass

public Class getContextClass()
Return the custom context class.

getContextConfigLocation

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

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".

getServletContextAttributeName

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

getWebApplicationContext

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

isPublishContext

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

setContextClass

public final void setContextClass(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)

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.

setNamespace

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

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.

Association Links

to Class java.lang.String

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'.

to Class java.lang.Class

Default context class for FrameworkServlet.

see org.springframework.web.context.support.XmlWebApplicationContext

to Class java.lang.String

Prefix for the ServletContext attribute for the web application context. The completion is the servlet name.

to Class java.lang.Class

Custom context class

to Class java.lang.String

Namespace for this servlet

to Class java.lang.String

Explicit context config location

to Class org.springframework.web.context.WebApplicationContext

WebApplicationContext for this servlet