org.apache.struts.action
Class ActionServlet

public class ActionServlet
extends HttpServlet

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 RequestProcessor object. This component represents the "controller" component of an MVC architecture.
  • The RequestProcessor selects and invokes an Action class to perform the requested business logic, or delegates the response to another resource.
  • The Action classes 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, Action classes generally return an ActionForward to indicate which resource should handle the response. If the Action does not return null, the RequestProcessor forwards or redirects to the specified resource (by utilizing RequestDispatcher.forward or Response.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 Action interface).
  • If this is the first request for a particular Action class, instantiate an instance of that class and cache it for future use.
  • Optionally populate the properties of an ActionForm bean associated with this mapping.
  • Call the execute method of this Action class, 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 ModuleConfigFactory used to create the implementation of the ModuleConfig interface. [org.apache.struts.config.impl.DefaultModuleConfigFactory]
  • 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.digester.RuleSet instances that should be added to the Digester that will be processing struts-config.xml files. By default, only the RuleSet for 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]
SinceNot specified.
Version$Rev: 2646
AuthorNot specified.
Wiki javadoc Use textile entry format.
Add your comments here.
Field Summary
protected String config

Comma-separated list of context-relative path(s) to our configuration resource(s) for the default module.

protected Digester configDigester

The Digester used to produce ModuleConfig objects from a Struts configuration file.

protected boolean convertNull

The flag to request backwards-compatible conversions for form bean properties of the Java wrapper class types.

protected FastHashMap dataSources

The JDBC data sources that has been configured for this module, if any, keyed by the servlet context attribute under which they are stored.

protected MessageResources internal

The resources object for our internal resources.

protected String internalName

The Java base name of our internal resources.

protected static Log log

Commons Logging instance.

protected RequestProcessor processor

The RequestProcessor instance we will use to process all incoming requests.

protected String[] registrations

The set of public identifiers, and corresponding resource names, for the versions of the configuration file DTDs that we know about.

protected String servletMapping

The URL pattern to which we are mapped in our web application deployment descriptor.

protected String servletName

The servlet name under which we are registered in our web application deployment descriptor.

Constructor Summary
ActionServlet()
No description provided.
Method Summary
void addServletMapping( String servletName, String urlPattern )

Remember a servlet mapping from our web application deployment descriptor, if it is for this servlet.

void destroy()

Gracefully shut down this controller servlet, releasing any resources that were allocated at initialization.

protected void destroyConfigDigester()

Gracefully release any configDigester instance that we have created.

protected void destroyInternal()

Gracefully terminate use of the internal MessageResources.

protected void destroyModules()

Gracefully terminate use of any modules associated with this application (if any).

void doGet( HttpServletRequest request, HttpServletResponse response )

Process an HTTP "GET" request.

void doPost( HttpServletRequest request, HttpServletResponse response )

Process an HTTP "POST" request.

MessageResources getInternal()

Return the MessageResources instance containing our internal message strings.

protected ModuleConfig getModuleConfig( HttpServletRequest request )

Return the module configuration object for the currently selected module.

protected RequestProcessor getRequestProcessor( ModuleConfig config )

Look up and return the RequestProcessor responsible for the specified module, creating a new one if necessary.

void init()

Initialize this servlet.

protected Digester initConfigDigester()

Create (if needed) and return a new Digester instance that has been initialized to process Struts module configuration files and configure a corresponding ModuleConfig object (which must be pushed on to the evaluation stack before parsing begins).

protected void initInternal()

Initialize our internal MessageResources bundle.

protected ModuleConfig initModuleConfig( String prefix, String paths )

Initialize the module configuration information for the specified module.

protected void initModuleConfigFactory()

Initialize the factory used to create the module configuration.

protected void initModuleDataSources( ModuleConfig config )

Initialize the data sources for the specified module.

protected void initModuleMessageResources( ModuleConfig config )

Initialize the application MessageResources for the specified module.

protected void initModulePlugIns( ModuleConfig config )

Initialize the plug ins for the specified module.

protected void initModulePrefixes( ServletContext context )

Saves a String[] of module prefixes in the ServletContext under Globals.MODULE_PREFIXES_KEY.

protected void initOther()

Initialize other global characteristics of the controller servlet.

protected void initServlet()

Initialize the servlet mapping under which our controller servlet is being accessed.

protected void parseModuleConfigFile( Digester digester, String path )

Parses one module config file.

protected void process( HttpServletRequest request, HttpServletResponse response )

Perform the standard request processing for this request, and create the corresponding response.

config
protected String config

Comma-separated list of context-relative path(s) to our configuration resource(s) for the default module.

Wiki javadoc Use textile entry format.
Add your comments here.
configDigester
protected Digester configDigester

The Digester used to produce ModuleConfig objects from a Struts configuration file.

Since: Struts 1.1
Wiki javadoc Use textile entry format.
Add your comments here.
convertNull
protected boolean convertNull

The flag to request backwards-compatible conversions for form bean properties of the Java wrapper class types.

Since: Struts 1.1
Wiki javadoc Use textile entry format.
Add your comments here.
dataSources
protected FastHashMap dataSources

The JDBC data sources that has been configured for this module, if any, keyed by the servlet context attribute under which they are stored.

Wiki javadoc Use textile entry format.
Add your comments here.
internal
protected MessageResources internal

The resources object for our internal resources.

Wiki javadoc Use textile entry format.
Add your comments here.
internalName
protected String internalName

The Java base name of our internal resources.

Since: Struts 1.1
Wiki javadoc Use textile entry format.
Add your comments here.
log
protected static Log log

Commons Logging instance.

Since: Struts 1.1
Wiki javadoc Use textile entry format.
Add your comments here.
processor
protected RequestProcessor processor

The RequestProcessor instance we will use to process all incoming requests.

Since: Struts 1.1
Wiki javadoc Use textile entry format.
Add your comments here.
registrations
protected String[] 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!

Wiki javadoc Use textile entry format.
Add your comments here.
servletMapping
protected String servletMapping

The URL pattern to which we are mapped in our web application deployment descriptor.

Wiki javadoc Use textile entry format.
Add your comments here.
servletName
protected String servletName

The servlet name under which we are registered in our web application deployment descriptor.

Wiki javadoc Use textile entry format.
Add your comments here.
ActionServlet
public ActionServlet ( )
No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
addServletMapping
public void addServletMapping ( String servletName, String urlPattern )

Remember a servlet mapping from our web application deployment descriptor, if it is for this servlet.

Parameters
TypeNameDescription
String servletName The name of the servlet being mapped
String urlPattern The URL pattern to which this servlet is mapped
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
destroy
public void destroy ( )

Gracefully shut down this controller servlet, releasing any resources that were allocated at initialization.

Overrides method in GenericServlet
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
destroyConfigDigester
protected void destroyConfigDigester ( )

Gracefully release any configDigester instance that we have created.

Since: Struts 1.1
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
destroyInternal
protected void destroyInternal ( )

Gracefully terminate use of the internal MessageResources.

Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
destroyModules
protected void destroyModules ( )

Gracefully terminate use of any modules associated with this application (if any).

Since: Struts 1.1
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
doGet
public void doGet ( HttpServletRequest request, HttpServletResponse response )

Process an HTTP "GET" request.

Overrides method in HttpServlet
Parameters
TypeNameDescription
HttpServletRequest request The servlet request we are processing
HttpServletResponse response The servlet response we are creating
Returns void No description provided.
Exceptions
IOException if an input/output error occurs
ServletException if a servlet exception occurs
Wiki javadoc Use textile entry format.
Add your comments here.
doPost
public void doPost ( HttpServletRequest request, HttpServletResponse response )

Process an HTTP "POST" request.

Overrides method in HttpServlet
Parameters
TypeNameDescription
HttpServletRequest request The servlet request we are processing
HttpServletResponse response The servlet response we are creating
Returns void No description provided.
Exceptions
IOException if an input/output error occurs
ServletException if a servlet exception occurs
Wiki javadoc Use textile entry format.
Add your comments here.
getInternal
public MessageResources getInternal ( )

Return the MessageResources instance containing our internal message strings.

Since: Struts 1.1
Wiki javadoc Use textile entry format.
Add your comments here.
getModuleConfig
protected ModuleConfig getModuleConfig ( HttpServletRequest request )

Return the module configuration object for the currently selected module.

Since: Struts 1.1
Parameters
TypeNameDescription
HttpServletRequest request The servlet request we are processing
Wiki javadoc Use textile entry format.
Add your comments here.
getRequestProcessor
protected RequestProcessor getRequestProcessor ( ModuleConfig config )

Look up and return the RequestProcessor responsible for the specified module, creating a new one if necessary.

Since: Struts 1.1
Parameters
TypeNameDescription
ModuleConfig config The module configuration for which to acquire and return a RequestProcessor.
Exceptions
ServletException if we cannot instantiate a RequestProcessor instance
Wiki javadoc Use textile entry format.
Add your comments here.
init
public void 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 method in GenericServlet
Returns void No description provided.
Exceptions
ServletException if we cannot configure ourselves correctly
Wiki javadoc Use textile entry format.
Add your comments here.
initConfigDigester
protected Digester initConfigDigester ( )

Create (if needed) and return a new Digester instance that has been initialized to process Struts module configuration files and configure a corresponding ModuleConfig object (which must be pushed on to the evaluation stack before parsing begins).

Since: Struts 1.1
Exceptions
ServletException if a Digester cannot be configured
Wiki javadoc Use textile entry format.
Add your comments here.
initInternal
protected void initInternal ( )

Initialize our internal MessageResources bundle.

Returns void No description provided.
Exceptions
ServletException if we cannot initialize these resources
Wiki javadoc Use textile entry format.
Add your comments here.
initModuleConfig
protected ModuleConfig initModuleConfig ( String prefix, String paths )

Initialize the module configuration information for the specified module.

Since: Struts 1.1
Parameters
TypeNameDescription
String prefix Module prefix for this module
String paths Comma-separated list of context-relative resource path(s) for this modules's configuration resource(s)
Exceptions
ServletException if initialization cannot be performed
Wiki javadoc Use textile entry format.
Add your comments here.
initModuleConfigFactory
protected void initModuleConfigFactory ( )

Initialize the factory used to create the module configuration.

Since: Struts 1.2
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
initModuleDataSources
protected void initModuleDataSources ( ModuleConfig config )

Initialize the data sources for the specified module.

Since: Struts 1.1
Parameters
TypeNameDescription
ModuleConfig config ModuleConfig information for this module
Returns void No description provided.
Exceptions
ServletException if initialization cannot be performed
Wiki javadoc Use textile entry format.
Add your comments here.
initModuleMessageResources
protected void initModuleMessageResources ( ModuleConfig config )

Initialize the application MessageResources for the specified module.

Since: Struts 1.1
Parameters
TypeNameDescription
ModuleConfig config ModuleConfig information for this module
Returns void No description provided.
Exceptions
ServletException if initialization cannot be performed
Wiki javadoc Use textile entry format.
Add your comments here.
initModulePlugIns
protected void initModulePlugIns ( ModuleConfig config )

Initialize the plug ins for the specified module.

Since: Struts 1.1
Parameters
TypeNameDescription
ModuleConfig config ModuleConfig information for this module
Returns void No description provided.
Exceptions
ServletException if initialization cannot be performed
Wiki javadoc Use textile entry format.
Add your comments here.
initModulePrefixes
protected void initModulePrefixes ( ServletContext context )

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.

Since: Struts 1.2
Parameters
TypeNameDescription
ServletContext context The servlet context.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
initOther
protected void initOther ( )

Initialize other global characteristics of the controller servlet.

Returns void No description provided.
Exceptions
ServletException if we cannot initialize these resources
Wiki javadoc Use textile entry format.
Add your comments here.
initServlet
protected void 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.

Returns void No description provided.
Exceptions
ServletException if error happens while scanning web.xml
Wiki javadoc Use textile entry format.
Add your comments here.
parseModuleConfigFile
protected void parseModuleConfigFile ( Digester digester, String path )

Parses one module config file.

Since: Struts 1.2
Parameters
TypeNameDescription
Digester digester Digester instance that does the parsing
String path The path to the config file to parse.
Returns void No description provided.
Exceptions
UnavailableException if file cannot be read or parsed
Wiki javadoc Use textile entry format.
Add your comments here.
process
protected void process ( HttpServletRequest request, HttpServletResponse response )

Perform the standard request processing for this request, and create the corresponding response.

Parameters
TypeNameDescription
HttpServletRequest request The servlet request we are processing
HttpServletResponse response The servlet response we are creating
Returns void No description provided.
Exceptions
IOException if an input/output error occurs
ServletException if a servlet exception is thrown
Wiki javadoc Use textile entry format.
Add your comments here.