org.springframework.web.servlet
Class HttpServletBean

java.lang.Object
  extended by HttpServlet
      extended by org.springframework.web.servlet.HttpServletBean
Direct Known Subclasses:
FrameworkServlet, ResourceServlet

public abstract class HttpServletBean
extends HttpServlet

Simple extension of javax.servlet.http.HttpServlet which treats its config parameters (init-param entries within the servlet tag in web.xml) as bean properties.

A handy superclass for any type of servlet. Type conversion of config parameters is automatic, with the corresponding setter method getting invoked with the converted value. It is also possible for subclasses to specify required properties. Parameters without matching bean property setter will simply be ignored.

This servlet leaves request handling to subclasses, inheriting the default behavior of HttpServlet (doGet, doPost, etc).

This generic servlet base class has no dependency on the Spring ApplicationContext concept. Simple servlets usually don't load their own context but rather access service beans from the Spring root application context, accessible via the filter's ServletContext (see WebApplicationContextUtils).

The FrameworkServlet class is a more specific servlet base class which loads its own application context. FrameworkServlet serves as direct base class of Spring's full-fledged DispatcherServlet.

Author:
Rod Johnson, Juergen Hoeller
See Also:
addRequiredProperty(java.lang.String), initServletBean(), #doGet, #doPost

Nested Class Summary
private static class HttpServletBean.ServletConfigPropertyValues
          PropertyValues implementation created from ServletConfig init parameters.
 
Field Summary
protected  Log logger
          Logger available to subclasses
private  java.util.Set<java.lang.String> requiredProperties
          Set of required properties (Strings) that must be supplied as config parameters to this servlet.
 
Constructor Summary
HttpServletBean()
           
 
Method Summary
protected  void addRequiredProperty(java.lang.String property)
          Subclasses can invoke this method to specify that this property (which must match a JavaBean property they expose) is mandatory, and must be supplied as a config parameter.
 ServletContext getServletContext()
          Overridden method that simply returns null when no ServletConfig set yet.
 java.lang.String getServletName()
          Overridden method that simply returns null when no ServletConfig set yet.
 void init()
          Map config parameters onto bean properties of this servlet, and invoke subclass initialization.
protected  void initBeanWrapper(BeanWrapper bw)
          Initialize the BeanWrapper for this HttpServletBean, possibly with custom editors.
protected  void initServletBean()
          Subclasses may override this to perform custom initialization.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger
Logger available to subclasses


requiredProperties

private final java.util.Set<java.lang.String> requiredProperties
Set of required properties (Strings) that must be supplied as config parameters to this servlet.

Constructor Detail

HttpServletBean

public HttpServletBean()
Method Detail

addRequiredProperty

protected final void addRequiredProperty(java.lang.String property)
Subclasses can invoke this method to specify that this property (which must match a JavaBean property they expose) is mandatory, and must be supplied as a config parameter. This should be called from the constructor of a subclass.

This method is only relevant in case of traditional initialization driven by a ServletConfig instance.

Parameters:
property - name of the required property

init

public final void init()
                throws ServletException
Map config parameters onto bean properties of this servlet, and invoke subclass initialization.

Throws:
ServletException - if bean properties are invalid (or required properties are missing), or if subclass initialization fails.

initBeanWrapper

protected void initBeanWrapper(BeanWrapper bw)
                        throws BeansException
Initialize the BeanWrapper for this HttpServletBean, possibly with custom editors.

This default implementation is empty.

Parameters:
bw - the BeanWrapper to initialize
Throws:
BeansException - if thrown by BeanWrapper methods
See Also:
PropertyEditorRegistry.registerCustomEditor(java.lang.Class, java.beans.PropertyEditor)

getServletName

public final java.lang.String getServletName()
Overridden method that simply returns null when no ServletConfig set yet.

See Also:
#getServletConfig()

getServletContext

public final ServletContext getServletContext()
Overridden method that simply returns null when no ServletConfig set yet.

See Also:
#getServletConfig()

initServletBean

protected void initServletBean()
                        throws ServletException
Subclasses may override this to perform custom initialization. All bean properties of this servlet will have been set before this method is invoked.

This default implementation is empty.

Throws:
ServletException - if subclass initialization fails