Class ActionServlet
- All Implemented Interfaces:
Servlet,ServletConfig,Serializable
ActionServlet provides the "controller" in the Model-View-Controller (MVC) design pattern for web applications that is commonly known as "Model 2". This nomenclature originated with a description in the JavaServerPages Specification, version 0.92, and has persisted ever since (in the absence of a better name).
Generally, a "Model 2" application is architected as follows:
- The user interface will generally be created with server pages, which will not themselves contain any business logic. These pages represent the "view" component of an MVC architecture.
- Forms and hyperlinks in the user interface that require business logic to be executed will be submitted to a request URI that is mapped to this servlet.
- There can be one instance of this servlet class, which receives
and processes all requests that change the state of a user's interaction
with the application. The servlet delegates the handling of a request to a
RequestProcessorobject. This component represents the "controller" component of an MVC architecture. - The
RequestProcessorselects and invokes anActionclass to perform the requested business logic, or delegates the response to another resource. - The
Actionclasses can manipulate the state of the application's interaction with the user, typically by creating or modifying JavaBeans that are stored as request or session attributes (depending on how long they need to be available). Such JavaBeans represent the "model" component of an MVC architecture. - Instead of producing the next page of the user interface directly,
Actionclasses generally return anActionForwardto indicate which resource should handle the response. If theActiondoes not return null, theRequestProcessorforwards or redirects to the specified resource (by utilizingRequestDispatcher.forwardorResponse.sendRedirect) so as to produce the next page of the user interface.
The standard version of RequestsProcessor implements the
following logic for each incoming HTTP request. You can override some or
all of this functionality by subclassing this object and implementing your
own version of the processing.
- Identify, from the incoming request URI, the substring that will be used to select an action procedure.
- Use this substring to map to the Java class name of the corresponding
action class (an implementation of the
Actioninterface). - If this is the first request for a particular
Actionclass, instantiate an instance of that class and cache it for future use. - Optionally populate the properties of an
ActionFormbean associated with this mapping. - Call the
executemethod of thisActionclass, passing on a reference to the mapping that was used, the relevant form-bean (if any), and the request and the response that were passed to the controller by the servlet container (thereby providing access to any specialized properties of the mapping itself as well as to the ServletContext).
The standard version of ActionServlet is configured based
on the following servlet initialization parameters, which you will specify
in the web application deployment descriptor (/WEB-INF/web.xml)
for your application. Subclasses that specialize this servlet are free to
define additional initialization parameters.
- config - Comma-separated list of context-relative path(s) to the XML resource(s) containing the configuration information for the default module. (Multiple files support since Struts 1.1) [/WEB-INF/struts-config.xml].
- config/${module} - Comma-separated list of Context-relative path(s) to the XML resource(s) containing the configuration information for the module that will use the specified prefix (/${module}). This can be repeated as many times as required for multiple modules. (Since Struts 1.1)
- configFactory - The Java class name of the
ModuleConfigFactoryused to create the implementation of the ModuleConfig interface. - convertNull - Force simulation of the Struts 1.0
behavior when populating forms. If set to true, the numeric Java wrapper
class types (like
java.lang.Integer) will default to null (rather than 0). (Since Struts 1.1) [false] - rulesets - Comma-delimited list of fully qualified
classnames of additional
org.apache.commons.digester3.RuleSetinstances that should be added to theDigesterthat will be processingstruts-config.xmlfiles. By default, only theRuleSetfor the standard configuration elements is loaded. (Since Struts 1.1) - validating - Should we use a validating XML parser to process the configuration file (strongly recommended)? [true]
- chainConfig - Comma-separated list of either context-relative or classloader path(s) to load commons-chain catalog definitions from. If none specified, the default Struts catalog that is provided with Struts will be used.
- Version:
- $Rev$ $Date: 2005-10-14 19:54:16 -0400 (Fri, 14 Oct 2005) $
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringComma-separated list of context or classloader-relative path(s) that contain the configuration for the default commons-chain catalog(s).protected StringComma-separated list of context-relative path(s) to our configuration resource(s) for the default module.protected org.apache.commons.digester.DigesterThe Digester used to produce ModuleConfig objects from a Struts configuration file.protected booleanThe flag to request backwards-compatible conversions for form bean properties of the Java wrapper class types.protected MessageResourcesThe resources object for our internal resources.protected StringThe Java base name of our internal resources.protected String[]The set of public identifiers, and corresponding resource names, for the versions of the configuration file DTDs that we know about.protected StringThe URL pattern to which we are mapped in our web application deployment descriptor.protected StringThe servlet name under which we are registered in our web application deployment descriptor. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddServletMapping(String servletName, String urlPattern) Remember a servlet mapping from our web application deployment descriptor, if it is for this servlet.voiddestroy()Gracefully shut down this controller servlet, releasing any resources that were allocated at initialization.protected voidGracefully release any configDigester instance that we have created.protected voidGracefully terminate use of the internal MessageResources.protected voidGracefully terminate use of any modules associated with this application (if any).voiddoGet(HttpServletRequest request, HttpServletResponse response) Process an HTTP "GET" request.voiddoPost(HttpServletRequest request, HttpServletResponse response) Process an HTTP "POST" request.Return theMessageResourcesinstance containing our internal message strings.protected ModuleConfiggetModuleConfig(HttpServletRequest request) Return the module configuration object for the currently selected module.protected RequestProcessorgetRequestProcessor(ModuleConfig config) Look up and return theRequestProcessorresponsible for the specified module, creating a new one if necessary.voidinit()Initialize this servlet.protected voidParse the configuration documents specified by thechainConfiginit-param to configure the defaultCatalogthat is registered in theCatalogFactoryinstance for this application.protected org.apache.commons.digester.DigesterCreate (if needed) and return a newDigesterinstance that has been initialized to process Struts module configuration files and configure a correspondingModuleConfigobject (which must be pushed on to the evaluation stack before parsing begins).protected voidInitialize our internal MessageResources bundle.protected voidinitModuleActions(ModuleConfig config) Initialize the action configs for the specified module.protected ModuleConfiginitModuleConfig(String prefix, String paths) Initialize the module configuration information for the specified module.protected voidInitialize the factory used to create the module configuration.protected voidInitialize the exception handlers for the specified module.protected voidinitModuleFormBeans(ModuleConfig config) Initialize the form beans for the specified module.protected voidinitModuleForwards(ModuleConfig config) Initialize the forwards for the specified module.protected voidInitialize the applicationMessageResourcesfor the specified module.protected voidinitModulePlugIns(ModuleConfig config) Initialize the plug ins for the specified module.protected voidinitModulePrefixes(ServletContext context) Saves a String[] of module prefixes in the ServletContext under Globals.MODULE_PREFIXES_KEY.protected voidInitialize other global characteristics of the controller servlet.protected voidInitialize the servlet mapping under which our controller servlet is being accessed.protected voidparseModuleConfigFile(org.apache.commons.digester.Digester digester, String path) Deprecated.use parseModuleConfigFile(Digester digester, URL url) insteadprotected voidparseModuleConfigFile(org.apache.commons.digester.Digester digester, URL url) Parses one module config file.protected voidprocess(HttpServletRequest request, HttpServletResponse response) Perform the standard request processing for this request, and create the corresponding response.protected ActionConfigprocessActionConfigClass(ActionConfig actionConfig, ModuleConfig moduleConfig) Checks if the current actionConfig is using the correct class based on the class of its ancestor ActionConfig.protected voidprocessActionConfigExtension(ActionConfig actionConfig, ModuleConfig moduleConfig) Extend the action's configuration as necessary.protected ExceptionConfigprocessExceptionConfigClass(ExceptionConfig exceptionConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) Checks if the current exceptionConfig is using the correct class based on the class of its configuration ancestor.protected voidprocessExceptionExtension(ExceptionConfig exceptionConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) Extend the exception's configuration as necessary.protected FormBeanConfigprocessFormBeanConfigClass(FormBeanConfig beanConfig, ModuleConfig moduleConfig) Checks if the current beanConfig is using the correct class based on the class of its ancestor form bean config.protected voidprocessFormBeanExtension(FormBeanConfig beanConfig, ModuleConfig moduleConfig) Extend the form bean's configuration as necessary.protected ForwardConfigprocessForwardConfigClass(ForwardConfig forwardConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) Checks if the current forwardConfig is using the correct class based on the class of its configuration ancestor.protected voidprocessForwardExtension(ForwardConfig forwardConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) Extend the forward's configuration as necessary.splitAndResolvePaths(String paths) Takes a comma-delimited string and splits it into paths, then resolves those paths using the ServletContext and appropriate ClassLoader.Methods inherited from class jakarta.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, serviceMethods inherited from class jakarta.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
Field Details
-
config
Comma-separated list of context-relative path(s) to our configuration resource(s) for the default module.
-
chainConfig
Comma-separated list of context or classloader-relative path(s) that contain the configuration for the default commons-chain catalog(s).
-
configDigester
protected org.apache.commons.digester.Digester configDigesterThe Digester used to produce ModuleConfig objects from a Struts configuration file.
- Since:
- Struts 1.1
-
convertNull
protected boolean convertNullThe flag to request backwards-compatible conversions for form bean properties of the Java wrapper class types.
- Since:
- Struts 1.1
-
internal
The resources object for our internal resources.
-
internalName
The Java base name of our internal resources.
- Since:
- Struts 1.1
-
registrations
The set of public identifiers, and corresponding resource names, for the versions of the configuration file DTDs that we know about. There MUST be an even number of Strings in this list!
-
servletMapping
The URL pattern to which we are mapped in our web application deployment descriptor.
-
servletName
The servlet name under which we are registered in our web application deployment descriptor.
-
-
Constructor Details
-
ActionServlet
public ActionServlet()
-
-
Method Details
-
destroy
public void destroy()Gracefully shut down this controller servlet, releasing any resources that were allocated at initialization.
- Specified by:
destroyin interfaceServlet- Overrides:
destroyin classGenericServlet
-
init
Initialize this servlet. Most of the processing has been factored into support methods so that you can override particular functionality at a fairly granular level.
- Overrides:
initin classGenericServlet- Throws:
ServletException- if we cannot configure ourselves correctly
-
initModulePrefixes
Saves a String[] of module prefixes in the ServletContext under Globals.MODULE_PREFIXES_KEY. NOTE - the "" prefix for the default module is not included in this list.
- Parameters:
context- The servlet context.- Since:
- Struts 1.2
-
doGet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException Process an HTTP "GET" request.
- Overrides:
doGetin classHttpServlet- Parameters:
request- The servlet request we are processingresponse- The servlet response we are creating- Throws:
IOException- if an input/output error occursServletException- if a servlet exception occurs
-
doPost
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException Process an HTTP "POST" request.
- Overrides:
doPostin classHttpServlet- Parameters:
request- The servlet request we are processingresponse- The servlet response we are creating- Throws:
IOException- if an input/output error occursServletException- if a servlet exception occurs
-
addServletMapping
Remember a servlet mapping from our web application deployment descriptor, if it is for this servlet.
- Parameters:
servletName- The name of the servlet being mappedurlPattern- The URL pattern to which this servlet is mapped
-
getInternal
Return the
MessageResourcesinstance containing our internal message strings.- Returns:
- the
MessageResourcesinstance containing our internal message strings. - Since:
- Struts 1.1
-
destroyModules
protected void destroyModules()Gracefully terminate use of any modules associated with this application (if any).
- Since:
- Struts 1.1
-
destroyConfigDigester
protected void destroyConfigDigester()Gracefully release any configDigester instance that we have created.
- Since:
- Struts 1.1
-
destroyInternal
protected void destroyInternal()Gracefully terminate use of the internal MessageResources.
-
getModuleConfig
Return the module configuration object for the currently selected module.
- Parameters:
request- The servlet request we are processing- Returns:
- The module configuration object for the currently selected module.
- Since:
- Struts 1.1
-
getRequestProcessor
Look up and return the
RequestProcessorresponsible for the specified module, creating a new one if necessary.- Parameters:
config- The module configuration for which to acquire and return a RequestProcessor.- Returns:
- The
RequestProcessorresponsible for the specified module, - Throws:
ServletException- If we cannot instantiate a RequestProcessor instance aUnavailableExceptionis thrown, meaning your application is not loaded and will not be available.- Since:
- Struts 1.1
-
initModuleConfigFactory
protected void initModuleConfigFactory()Initialize the factory used to create the module configuration.
- Since:
- Struts 1.2
-
initModuleConfig
Initialize the module configuration information for the specified module.
- Parameters:
prefix- Module prefix for this modulepaths- Comma-separated list of context-relative resource path(s) for this modules's configuration resource(s)- Returns:
- The new module configuration instance.
- Throws:
ServletException- if initialization cannot be performed- Since:
- Struts 1.1
-
parseModuleConfigFile
@Deprecated protected void parseModuleConfigFile(org.apache.commons.digester.Digester digester, String path) throws UnavailableException Deprecated.use parseModuleConfigFile(Digester digester, URL url) insteadParses one module config file.
- Parameters:
digester- Digester instance that does the parsingpath- The path to the config file to parse.- Throws:
UnavailableException- if file cannot be read or parsed- Since:
- Struts 1.2
-
parseModuleConfigFile
protected void parseModuleConfigFile(org.apache.commons.digester.Digester digester, URL url) throws UnavailableException Parses one module config file.
- Parameters:
digester- Digester instance that does the parsingurl- The url to the config file to parse.- Throws:
UnavailableException- if file cannot be read or parsed- Since:
- Struts 1.3
-
initModulePlugIns
Initialize the plug ins for the specified module.
- Parameters:
config- ModuleConfig information for this module- Throws:
ServletException- if initialization cannot be performed- Since:
- Struts 1.1
-
initModuleFormBeans
Initialize the form beans for the specified module.
- Parameters:
config- ModuleConfig information for this module- Throws:
ServletException- if initialization cannot be performed- Since:
- Struts 1.3
-
processFormBeanExtension
protected void processFormBeanExtension(FormBeanConfig beanConfig, ModuleConfig moduleConfig) throws ServletException Extend the form bean's configuration as necessary.
- Parameters:
beanConfig- the configuration to process.moduleConfig- the module configuration for this module.- Throws:
ServletException- if initialization cannot be performed.
-
processFormBeanConfigClass
protected FormBeanConfig processFormBeanConfigClass(FormBeanConfig beanConfig, ModuleConfig moduleConfig) throws ServletException Checks if the current beanConfig is using the correct class based on the class of its ancestor form bean config.
- Parameters:
beanConfig- The form bean to check.moduleConfig- The config for the current module.- Returns:
- The form bean config using the correct class as determined by the config's ancestor and its own overridden value.
- Throws:
UnavailableException- if an instance of the form bean config class cannot be created.ServletException- on class creation error
-
initModuleForwards
Initialize the forwards for the specified module.
- Parameters:
config- ModuleConfig information for this module- Throws:
ServletException- if initialization cannot be performed
-
processForwardExtension
protected void processForwardExtension(ForwardConfig forwardConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) throws ServletException Extend the forward's configuration as necessary. If actionConfig is provided, then this method will process the forwardConfig as part of that actionConfig. If actionConfig is null, the forwardConfig will be processed as a global forward.
- Parameters:
forwardConfig- the configuration to process.moduleConfig- the module configuration for this module.actionConfig- If applicable, the config for the current action.- Throws:
ServletException- if initialization cannot be performed.
-
processForwardConfigClass
protected ForwardConfig processForwardConfigClass(ForwardConfig forwardConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) throws ServletException Checks if the current forwardConfig is using the correct class based on the class of its configuration ancestor. If actionConfig is provided, then this method will process the forwardConfig as part of that actionConfig. If actionConfig is null, the forwardConfig will be processed as a global forward.
- Parameters:
forwardConfig- The forward to check.moduleConfig- The config for the current module.actionConfig- If applicable, the config for the current action.- Returns:
- The forward config using the correct class as determined by the config's ancestor and its own overridden value.
- Throws:
UnavailableException- if an instance of the forward config class cannot be created.ServletException- on class creation error
-
initModuleExceptionConfigs
Initialize the exception handlers for the specified module.
- Parameters:
config- ModuleConfig information for this module- Throws:
ServletException- if initialization cannot be performed- Since:
- Struts 1.3
-
processExceptionExtension
protected void processExceptionExtension(ExceptionConfig exceptionConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) throws ServletException Extend the exception's configuration as necessary. If actionConfig is provided, then this method will process the exceptionConfig as part of that actionConfig. If actionConfig is null, the exceptionConfig will be processed as a global forward.
- Parameters:
exceptionConfig- the configuration to process.moduleConfig- the module configuration for this module.actionConfig- If applicable, the config for the current action.- Throws:
ServletException- if initialization cannot be performed.
-
processExceptionConfigClass
protected ExceptionConfig processExceptionConfigClass(ExceptionConfig exceptionConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) throws ServletException Checks if the current exceptionConfig is using the correct class based on the class of its configuration ancestor. If actionConfig is provided, then this method will process the exceptionConfig as part of that actionConfig. If actionConfig is null, the exceptionConfig will be processed as a global forward.
- Parameters:
exceptionConfig- The config to check.moduleConfig- The config for the current module.actionConfig- If applicable, the config for the current action.- Returns:
- The exception config using the correct class as determined by the config's ancestor and its own overridden value.
- Throws:
ServletException- if an instance of the exception config class cannot be created.
-
initModuleActions
Initialize the action configs for the specified module.
- Parameters:
config- ModuleConfig information for this module- Throws:
ServletException- if initialization cannot be performed- Since:
- Struts 1.3
-
processActionConfigExtension
protected void processActionConfigExtension(ActionConfig actionConfig, ModuleConfig moduleConfig) throws ServletException Extend the action's configuration as necessary.
- Parameters:
actionConfig- the configuration to process.moduleConfig- the module configuration for this module.- Throws:
ServletException- if initialization cannot be performed.
-
processActionConfigClass
protected ActionConfig processActionConfigClass(ActionConfig actionConfig, ModuleConfig moduleConfig) throws ServletException Checks if the current actionConfig is using the correct class based on the class of its ancestor ActionConfig.
- Parameters:
actionConfig- The action config to check.moduleConfig- The config for the current module.- Returns:
- The config object using the correct class as determined by the config's ancestor and its own overridden value.
- Throws:
ServletException- if an instance of the action config class cannot be created.
-
initModuleMessageResources
Initialize the application
MessageResourcesfor the specified module.- Parameters:
config- ModuleConfig information for this module- Throws:
ServletException- if initialization cannot be performed- Since:
- Struts 1.1
-
initConfigDigester
Create (if needed) and return a new
Digesterinstance that has been initialized to process Struts module configuration files and configure a correspondingModuleConfigobject (which must be pushed on to the evaluation stack before parsing begins).- Returns:
- A new configured
Digesterinstance. - Throws:
ServletException- if a Digester cannot be configured- Since:
- Struts 1.1
-
initInternal
Initialize our internal MessageResources bundle.
- Throws:
ServletException- if we cannot initialize these resourcesUnavailableException- if we cannot load resources
-
initChain
Parse the configuration documents specified by the
chainConfiginit-param to configure the defaultCatalogthat is registered in theCatalogFactoryinstance for this application.- Throws:
ServletException- if an error occurs.
-
initOther
Initialize other global characteristics of the controller servlet.
- Throws:
ServletException- if we cannot initialize these resources
-
initServlet
Initialize the servlet mapping under which our controller servlet is being accessed. This will be used in the
<html:form>tag to generate correct destination URLs for form submissions.- Throws:
ServletException- if error happens while scanning web.xml
-
splitAndResolvePaths
Takes a comma-delimited string and splits it into paths, then resolves those paths using the ServletContext and appropriate ClassLoader. When loading from the classloader, multiple resources per path are supported to support, for example, multiple jars containing the same named config file.
- Parameters:
paths- A comma-delimited string of paths- Returns:
- A list of resolved URL's for all found resources
- Throws:
ServletException- if a servlet exception is thrown
-
process
protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException Perform the standard request processing for this request, and create the corresponding response.
- Parameters:
request- The servlet request we are processingresponse- The servlet response we are creating- Throws:
IOException- if an input/output error occursServletException- if a servlet exception is thrown
-