|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.servlet.GenericServlet javax.servlet.http.HttpServlet org.springframework.web.servlet.HttpServletBean org.springframework.web.servlet.FrameworkServlet
public abstract class FrameworkServlet
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.
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
initFrameworkServlet()
,
setContextClass(java.lang.Class)
,
setContextConfigLocation(java.lang.String)
,
setNamespace(java.lang.String)
,
Serialized FormField 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 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()
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 abstract 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 ServletRequestHandledEvent 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 ServletRequestHandledEvent at the end of each request. |
Methods inherited from class org.springframework.web.servlet.HttpServletBean |
---|
addRequiredProperty, getServletContext, getServletName, init, initBeanWrapper |
Methods inherited from class javax.servlet.http.HttpServlet |
---|
doHead, doOptions, doTrace, getLastModified, service, service |
Methods inherited from class javax.servlet.GenericServlet |
---|
getInitParameter, getInitParameterNames, getServletConfig, getServletInfo, init, log, log |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String DEFAULT_NAMESPACE_SUFFIX
public static final Class DEFAULT_CONTEXT_CLASS
XmlWebApplicationContext
public static final String SERVLET_CONTEXT_PREFIX
Constructor Detail |
---|
public FrameworkServlet()
Method Detail |
---|
public void setContextClass(Class contextClass)
createWebApplicationContext(org.springframework.web.context.WebApplicationContext)
public Class getContextClass()
public void setNamespace(String namespace)
public String getNamespace()
public void setContextConfigLocation(String contextConfigLocation)
public String getContextConfigLocation()
public void setPublishContext(boolean publishContext)
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.
public boolean isPublishContext()
public void setPublishEvents(boolean publishEvents)
ServletRequestHandledEvent
public boolean isPublishEvents()
protected final void initServletBean() throws ServletException, BeansException
initServletBean
in class HttpServletBean
ServletException
- if subclass initialization fails
BeansException
protected WebApplicationContext initWebApplicationContext() throws BeansException
BeansException
- if the context couldn't be initializedcreateWebApplicationContext(org.springframework.web.context.WebApplicationContext)
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException
parent
- the parent ApplicationContext to use, or null
if none
BeansException
- if the context couldn't be initializedsetContextClass(java.lang.Class)
,
XmlWebApplicationContext
public String getServletContextAttributeName()
SERVLET_CONTEXT_PREFIX
,
HttpServletBean.getServletName()
public final WebApplicationContext getWebApplicationContext()
protected void initFrameworkServlet() throws ServletException, BeansException
ServletException
- in case of an initialization exception
BeansException
- if thrown by ApplicationContext methodsprotected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
Will also be invoked by HttpServlet's default implementation of doHead, with a NoBodyResponse that just captures the content length.
doGet
in class HttpServlet
ServletException
IOException
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
HttpServlet.doHead(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
doPost
in class HttpServlet
ServletException
IOException
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected final void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
doPut
in class HttpServlet
ServletException
IOException
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected final void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
doDelete
in class HttpServlet
ServletException
IOException
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
doService()
template method.
ServletException
IOException
doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected String getUsernameForRequest(HttpServletRequest request)
request
- current HTTP request
null
if noneHttpServletRequest.getUserPrincipal()
protected abstract void doService(HttpServletRequest request, HttpServletResponse response) throws Exception
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.
request
- current HTTP requestresponse
- current HTTP response
Exception
- in case of any kind of processing failureHttpServlet.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
HttpServlet.doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
public void destroy()
destroy
in interface Servlet
destroy
in class GenericServlet
ConfigurableApplicationContext.close()
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |