Front Controller – Struts Tutorial

Front Controller:-

A special web resource program in the web application i.e capable of trapping & taking request coming to other web resource programs of web application is called as front controller.

Front controller contains the common & global prerequest process logic like authentication & authorization logics.

Servlet or JSP program becomes front controller only when it contains extension or directory match URL patterns.

  • Front controller is not responsible to trap the response generated by other web resource programs. But it is responsible to trap request going to other web resource programs.
  • The servlet filter program is capable of trapping both request & response of other web resource programs.
  • In struts application Action Servlet in configured as front controller servlet for this extension match, directory matches URL pattern should be specified for Action Servlet in web.xml file.
  • Even through Action Servlet is built in servlet its configuration in web.xml file having extension match or directory match URL pattern is the responsibility of program.

Question:- Why Action Servlet should be configured as front controller?

  1. Struts Action class is an ordinary java class so it cannot take http request directly from the clients even though it contains business logic to process the request. To overcome these problems we make Action Servlet as mediator to take http request from browser window and to pass that request to an appropriate action class. To make this Action Servlet trapping & taking the entire request coming to all the Action classes of Struts application it must be configured as “front controller”.
  • Programmers generally configure Action Servlet in web.xml file as shown below.

In web.xml:-

     <web-app>

         <servlet>

            <servlet-name>action</servlet-name>

            <servlet-class>org.apache.struts.action.Actionservlet</servlet-class>

            <init-param>

               <param-name>config</param-name>

               <param-values>/web-inf/struts-config.xml</param-value>

            </init-param>

            <load – on -startup>2</load –on – startup>

        </servlet>

        <servlet-mapping>

               <s-n> action </s-n>

               <u-p>*.do</u-p>

         </servlet-mapping>

     </web-app>

  • As a front controller Action Servlet traps the request coming from the clients to Action classes (like browsers window). As a controller of struts based & MVC2 Architecture based web application it traps the request given by Action class and passes them to result jsp pages of view layer.
  • There are 2 ways pass input values to servlet program from outside the servlet program.
  1. Request parameters ( from client side )
  2. Init parameters / context parameters ( from server side )
  • Use request parameters to send non technical input values to servlet program from browser window. (as from data)
  • Use Init / context parameters to send technical input values from web.xml file by programmer. (like jdbc driver details)
  • Since the name & location of struts-configuration file is technical input value required by Action Servlet, so we use the Init parameter “config” to supply that value during Action Servlet configuration (refer above web.xml)
  • When load-on-startup is not enable on a servlet program the first request to given servlet program participate in instantiation & initialization process before processing request by executing service method where as the other first request given to servlet program directly passes the request by calling service method.

Due to this the response time of first request will be little bit high when you compare with the response time of other than first request.

To equalize with response time other than first request make sevlet container perform instantiation & initialization operation on servlet program during server startup by enabling load-on-startup on servlet. So the first request given to servlet program directly executes service method & process the request.

  • It is recommended to enable i-o-s Action Servlet to equalize response time of first request time of other than first request.
  • When multiple servlets of web app’s are enable with L-O-S in which order their object will be created either during server startup or during the deployment of web application will be decided based on their I-O-S priority values. High value gives low priority. Low values give high priority. Zero (0) gives least priority. Negative values give ignored the enabled <I-O-S>.

Note: – In Tomcat 6.0 for zero there is no separate meaning for I-O-S priority. Enable in <L-O-S> on a servlet program with –ve value is equal to not enabling L-O-S on servlet program.

Classic web application (other than Tomcat 6.0):-

                      <L-O-S>value

Srv 1                   4 (3)

Srv 2                   3 (2)

Srv 3                   0 (4)

Srv 4                   1 (1)

Classic web application (other than Tomcat 6.0)

                       <I-O-S>value

Srv 1                   14 (0)

Srv 2                    -3 <L-O-S> is ignored

Srv 3                     2 (1)

Srv 4                     0 (3)

Classic web application (other than Tomcat 6.0)

                      <I-O-S>value

Srv 1                   3

Srv 2                    3

Srv 3                    3

Srv 4                    3

  • Since Action Servlet is the only one servlet of struts based web application the programmer can use any positive number or zero as <I-O-S> priority value while configuring Action Servlet in web.xml file.

Leave a Reply

Your email address will not be published. Required fields are marked *