The Element of struts Architecture



1.       The Action Servlet:  The Action Servlet of struts framwwork is an implementation of front controller pattern and java servlet component i.e is a servlet front for struts application. The ActionServlet is a subtype of javax.servlet.http.HttpServlet and implements init(), doGet(), doPost() and destroy() methods. The ActionServlet init() method is responsible to find the user configured struts modules and locate the struts configuration file of each module, then read, validate the configuration and load the details into moduleconfig object which is further injected into the module specific RequestProcessor instance.

The RequestProcessor further ses the ModuleConfig to get all the moule specific details. Once if all these initializations are successfully done then the ActionServlet instance is put into service, which provides an entry point to serve the client requests using these configurations loaded. This means that once if the ActionServlet is initialized any chages into the configuration files are not affected until the ActionServlet is reinitialized which can be done by redeploying the containing web application.

The ActionServlet accepts HTTP GET and POST method request where the doGet() and doPost() method implementations of ActionServlet just call the protected process() method of the same class where all the request handling logic is  implemented.

The ActionServlet process() method identifies the module that client has requested for , this is done by matching the first part of the request URL with the configured modules. There after it gets the RequestProcessor instance of the respecting module and delegates the request to that RequestProcessor instance by invoking process() method on the located RequestProcessor instance.

Configuring Action-Servlet

As we have discussed in the above section that the ActionSerlvet class in the front controller class that recives all incoming HTTP client requests for the application. In addition, ActionSerlvet is responsible for initializing the struts frame work for out application, and is a servlet extending HttpServlet just like any other servlet and thus ActionSerlvet is also required to be configured in out web application like any other servlet i.e inot Web application deployment descriptor (web.xml). Since ActionServlet has to be configured to receive all the requests to out application i.e for various services. ActionSerlvet’s mapping should be generic and there are two approaches to configure ActionServlet to recive all requests for our application. First, Action Servlet can be configured using path mapping, as shown in the following code snippet.

<web-app>
<!—ActionServlet Declarationà
 <Servlet>
            <Servlet-name>action</servlet-name>
            <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
            <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-ING/Struts-config.xml</param-value>
            <load-on-startup>1</load-on-startup>
<servlet>
<!--- Action Servlet Mapping>
<servlet-mapping>
  <Servlet-name>action</servlet-name>
 <url-pattern./do/*</url-pattern>
</servlet-mapping>
</web-app>

In this case all the request for this context whose servletPath starts with / do are dispatched by the web container to the ActionServlet which further takes the responsibility to handle the request as explained in the above section. As shown in the above code it is not mandatory to confirure only /do/* instead we are allowed to configure any other path mapping also.

The second approach to configure the mapping for ActionServlet is to use extension mapping as shown in the below give code snippet.

<web-app>
<!—ActionServlet Declarationà
 <Servlet>
            <Servlet-name>action</servlet-name>
            <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
            <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-ING/Struts-config.xml</param-value>
            <load-on-startup>1</load-on-startup>
<servlet>
<!--- Action Servlet Mapping>
<servlet-mapping>
  <Servlet-name>action</servlet-name>
 <url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

In this case all the request for this context whose servletPath ends with .do extension are dispatched by the web container to the ActionServlet which further takes the responsibility to handle the request as explained in the above section. As shown in the above code its is not mandatory to configure only .do instead we are allowed to configured any other textension also excluding the reserved extension like jsp,jspx,sun,java etc.

NextPost Struts configuration file and more component………………….
 
Top