FrontController web.xml Servlet

The FrontController acts as if it is a filter, we want all requests--whether for static HTML pages, JSPs, or Servlets--to go to the controller when they first come into the system.

Important Note:Request generated with the forward()method of the RequestDispatcher must not go to the FrontController otherwise an infinite loop is created.

The RequestDispatcher generates requests as if they came from outside the system. If we simply deploy the FrontController to intercept all request, it will cause a loop. Instead create a convention: any URL beginning with /pages will invoke the FrontController. When a FrontController does a forward, it simply removes the prefix, meaning that any page can be accessed simply by putting the prefix in front of it.

The final step in deploying the FrontController is to edit the servers web.xml file to make sure the controller is called for any request beginning with /pages.

Script 1.0 FrontController web.xml Servlet

< ?xml version="1.0" encoding="UTF"? >
< !DOCTYPE web-app Public"-//Sun Microsystems, Inc.//DTD Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

< web-app>

.....

< servlet>
    < servlet-name>FrontController< /servlet-name>
    < servlet-class>FrontController< /servlet-class>
< /servlet>

< servlet-mapping>
    < servlet-name>FrontController< /servlet-name>
    < url-pattern>/pages/*< /url-pattern>
< /servlet-mapping>


.....

< /web-app>

When the controller does a forward, it simply removes the prefix, meaning that any page can be accessed by simply putting the prefix in front of it. In the form action /pages/models.html. In login.jsp the form action is /pages/home.html. Therefore, if the login is successful, the response will be generated by /home.html. We can achieve the same effect by choosing an extension instead of a prefix. In the that case /home.html.act might map to /home.html