Class HttpServletBean
- java.lang.Object
- javax.servlet.GenericServlet
- javax.servlet.http.HttpServlet
- org.springframework.web.servlet.HttpServletBean
- All Implemented Interfaces:
Serializable
,Servlet
,ServletConfig
,Aware
,EnvironmentAware
,EnvironmentCapable
- Direct Known Subclasses:
FrameworkServlet
,ResourceServlet
public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware
Simple extension ofHttpServlet
which treats its config parameters (init-param
entries within theservlet
tag inweb.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'sServletContext
(seeWebApplicationContextUtils
).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-fledgedDispatcherServlet
.- Author:
- Rod Johnson, Juergen Hoeller
- See Also:
addRequiredProperty(java.lang.String)
,initServletBean()
,HttpServlet.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,HttpServlet.doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
, Serialized Form
Constructor Summary
Constructors Constructor Description HttpServletBean()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addRequiredProperty(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.protected ConfigurableEnvironment
createEnvironment()
Create and return a newStandardServletEnvironment
.ConfigurableEnvironment
getEnvironment()
Return theEnvironment
associated with this servlet.ServletContext
getServletContext()
Overridden method that simply returnsnull
when no ServletConfig set yet.String
getServletName()
Overridden method that simply returnsnull
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.void
setEnvironment(Environment environment)
Set theEnvironment
that this servlet runs in.Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletInfo, init, log, log
Constructor Detail
HttpServletBean
public HttpServletBean()
Method Detail
addRequiredProperty
protected final void addRequiredProperty(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
setEnvironment
public void setEnvironment(Environment environment)
Set theEnvironment
that this servlet runs in.Any environment set here overrides the
StandardServletEnvironment
provided by default.- Specified by:
setEnvironment
in interfaceEnvironmentAware
- Throws:
IllegalArgumentException
- if environment is not assignable toConfigurableEnvironment
getEnvironment
public ConfigurableEnvironment getEnvironment()
Return theEnvironment
associated with this servlet.If none specified, a default environment will be initialized via
createEnvironment()
.- Specified by:
getEnvironment
in interfaceEnvironmentCapable
createEnvironment
protected ConfigurableEnvironment createEnvironment()
Create and return a newStandardServletEnvironment
.Subclasses may override this in order to configure the environment or specialize the environment type returned.
init
public final void init() throws ServletException
Map config parameters onto bean properties of this servlet, and invoke subclass initialization.- Overrides:
init
in classGenericServlet
- 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)
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
getServletName
public final String getServletName()
Overridden method that simply returnsnull
when no ServletConfig set yet.- Specified by:
getServletName
in interfaceServletConfig
- Overrides:
getServletName
in classGenericServlet
- See Also:
GenericServlet.getServletConfig()
getServletContext
public final ServletContext getServletContext()
Overridden method that simply returnsnull
when no ServletConfig set yet.- Specified by:
getServletContext
in interfaceServletConfig
- Overrides:
getServletContext
in classGenericServlet
- See Also:
GenericServlet.getServletConfig()