Search code examples
javastruts-1

How many instances of request processor can exist in web application?


I was reading some blogs for struts 1.x, and found that there can be only one instance of action servlet per web application and once instance of request processor per web application module.

Can someone explain me whats exactly mean by application module here? or how can we configure request processor for application module?


Solution

  • In all versions of Struts, including version 1.1, you can run only one instance of the ActionServlet controller servlet per Web application (i.e., .war file). This is because ActionServlet stores several details of the framework's configuration in variables within the Web application scope, which is the scope shared by all parts of the application. Thus, attempting to run multiple instances of ActionServlet in the same Web application causes a problem, because the data associated with one instance overwrites the data associated with another instance.

    Prior to version 1.1 your entire application's configuration had to reside in a single Struts configuration file. For large applications, where the configuration file gets very large and several developers are involved, that proved to be a problematic point of contention.

    To ovecome this issue, in later version, struts provides the functionality of having multiple instances by subdividing your application into discreet units by using modules. Each module has its own Struts configuration file, its own set of actions and forwards, a separate URL namespace, and so on. Essentially, modules are like mini-Struts applications inside a master Struts application. Although added in the same release (1.1) as modules, multiple configuration files can be used independently, without having to convert your application for use with modules. Support for multiple configuration files alleviates some of the problems associated with large, team-based development with Struts. However, multiple configuration files do not partition your Struts application the way that modules do. When using multiple configuration files, the information in the files is combined to create one large set of configuration data. This can cause a problem, however. For example, if you define an action with a certain name in one file and another configuration file defines another action with the same name, the configuration file last read will override the first file's data. Basically, last one in wins. Modules do not have this problem, because Struts stores each module's configuration data separately. In each configuration file, you can configure your custom request processor using controller tag.

    How to create a Struts Configuration File for Each Module?

    1. Create a separate Struts configuration file for each module -

      /WEB-INF/struts-config-moduleA.xml
      /WEB-INF/struts-config-moduleB.xml

    2. Configuring the web.xml Deployment Descriptor for Modules

      <!-- Action Servlet Configuration -->
      <servlet>
      <servlet-name>action</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
      <init-param>
         <param-name>config/moduleA</param-name>
         <param-value>/WEB-INF/struts-config-moduleA.xml</param-value>
       </init-param>
       <init-param>
          <param-name>config/moduleB</param-name>
          <param-value>/WEB-INF/struts-config-moduleB.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        </servlet>
      

    I am not sure if this answer your question, but this might be useful to you.

    If you want to extend RequestProcessor class, you need to do two thing -

    1. Extend RequestProcessor class. Example -

      public class CustomRequestProcessor extends RequestProcessor{
      // Do whatever you want
      }
      
    2. Create an entry for controller in struts-config.xml, Example -

      <controller contentType="text/html;charset=UTF-8" locale="true" 
      processorClass="fully qualified custom request processor class name" />