Inter application forwarding using ServletContext

In this post, you will learn:

  • What is inter application forwarding?
  • Why getRequestDispatcher() method is in both ServletRequest & ServletContext interfaces?
  • How an application can obtain the reference of ServletContext of another application?
  • How to configure tomcat to allow inter application forwarding?

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.

public RequestDispatcher getRequestDispatcher(String path);

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:

public ServletContext getContext(String appName);

Following diagram describes how a request can be forwarded from one application to another:

inter application forwarding

Description of the diagram:

1.0 & 2.0 – App1 & App2 are deployed on the server.

1.1 & 2.1 – ServletContext objects are created by the server for App1 & App2.

1.2 & 2.2 – References of the ServletContext objects of App1 & App2 are stored by the server in a Map.

3.0 – A client submits a request for S1 servlet of App1.

3.1 – The server invokes the service() method of S1 servlet.

3.2 – S1 servlet asks its own ServletContext to bring the reference of ServletContext of App2 from the server.

3.3 – ServletContext of App1 asks the server to provide it the reference of App2.

3.4 – The server fetches the reference of the ServletContext of App2 from its map.

3.5 – Reference of ServletContext of app2 is provided by the server to the ServletContext of App1.

3.6 – ServletContext of App1 provides the reference received from the server to S1 servlet.

3.7 – S1 servlet asks the ServletContext to provide a RequestDispatcher object for the S2 servlet.

3.8 – RequestDispatcher for S2 servlet is created by the ServletContext of app2.

3.9 – Reference of the RequestDispatcher is provided to the S1 servlet.

4.0 – S1 servlet invokes the forward() method on the RequestDispatcher.

4.1 – S2 servlet is invoked by the RequestDispatcher.

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.


Its time to recap the main concepts:

  1. Forwarding request from one application to another on the same server is called inter application forwarding.
  2. In case of inter application forwarding, RequestDispatcher can only be created by the ServletContext object.
  3. getContext() method of ServletContext interface is used to request the reference of ServletContext of another application from the server.
  4. By default, Tomcat doesn’t allow inter application forwarding.
  5. To enable inter application forwarding in Tomcat, crossContext attribute of context element need to be set to true.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

One thought on “Inter application forwarding using ServletContext

  1. Nice Content!
    It is very helpful and authentic content on Java and the best one I ever found online on Java technology.Every one should read this nice and quality based content.

    Best of luck for your work!

Leave a Reply

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