Usually web applications have different modules such as end user module, admin module, service module etc. These modules can be packaged in a single application or can be deployed as separate applications on the same server. If they are deployed as separate applications then they need to forward requests between them. Such forwarding of request between applications on the same server is called inter application forwarding.
In Java web applications, inter application forwarding is made possible by ServletContext. As discussed in the last post, a ServletContext object is created by the server for each application at the time of deployment. This object has the reference of the server i.e. it can communicate to the server on behalf of application components. To forward request from one application to another, ServletContext solves two important problems which are described by the following use case:
Let there be two applications deployed on a server named App1 & App2 having S1 & S2 servlets respectively. When a request is submitted to Servlet S1 of App1, it need to be forwarded to S2 servlet of App2.
Problems of inter application forwarding:
1.0 – To forward request to another component, a RequestDispatcher object is required. In the post of RequestDispatcher, you were told that a RequestDispatcher object can be obtained using the following method of ServletRequest interface.
The problem in this method is that the ServletRequest object can only create RequestDispatcher for a component of the application for which the request is received. In our use case, User submits requests for S1 servlet of App1 which means using the getRequestDispatcher() method of ServletRequest, RequestDispatcher can’t be created for S2 servlet of App2.
The getRequestDispatcher() method is also part of the ServletContext interface specially for such situations. Now we can obtain a RequestDispatcher object for the servlet S2 of App2 from the ServletContext object of App2.
2.0 – The solution of the first problem creates second problem. RequestDispatcher for S2 servlet, will be created by S1 servlet which is part of App1 i.e. it has the reference of ServletContext of App1 (through its ServletConfig). To create the RequestDispatcher for S2 servlet, Reference of the ServletContext of App2 is needed.
All the ServletContext objects are created by the server i.e The server has the references of the ServletContext objects of all the deployed applications. ServletContext of each application is provided the reference of the server which means that S1 servet can obtain the reference of the ServletContext of App2 from the server with the help of its own ServletContext object.
To request the reference of ServletContext object of an application from the server, ServletContext interface provides following method:
Following diagram describes how a request can be forwarded from one application to another:
Description of the diagram:
Before you can implement inter application forwarding as described above there is one more problem to be tackled. By default, Servlet container such as Tomcat don’t expose reference of one application to another. To enable, inter application forwarding in Tomcat, <Context> element of context.xml need to changed as follows:
<!-- set crossContext attribute of <Context> element to true i.e change default <Context> to --> <Context crossContext="true"> --- </Context>
That’s too much for a single post. The code for this example will be provided in the next post.