Inter-servlet Communication

Applets loaded in the same Web page can communicate with each other using the AppletContext object. Similarly, on the server-side, the Java servlet specification enables servlets loaded in the same instance of the server to communicate with each other. This communication between servlets is possible by using the only object available in a servlet that allows it to interact with the servlet container, the ServletContext object. The ServletContext object provides methods to access and communicate with the other loaded servlets using the RequestDispatcher object.

So why would you want to communicate with another servlet? Servlets can be reused for their functionality, like any other Java class. For example, a servlet that provides authentication can be reused by other servlets in the servlet container.

A servlet can do inter-servlet communication in the following ways after obtaining the reference to another servlet by using the ServletContext object:

  • Shared object between the two servlets

  • Servlet chaining using the RequestDispatcher

Shared Object

The simplest and easiest way of communicating between two servlets is by using a shared class. A shared object is essentially a Java object defined as a public variable in one of the servlets. The Java object contains member variables (private or public) and accessor or mutator methods (get() or set()) to set and retrieve the values of the member variables. A servlet wishing to pass data to another servlet needs to get a reference to this shared object contained in the other servlet. Following is a code snippet that does this:

 ...  // assume that the ServletContext, in this case "sc" has been obtained in // the init() method // obtain the reference to the other servlet "otherServlet" OtherServlet otherServlet = (OtherServlet) sc.getServlet(OtherServlet); // retrieve the shared object mySharedObj Object mySharedObj = otherServlet.getSharedObject(); // set the values on the shared object myShareObj.setData("shared data"); ... 

Servlet Chaining

Servlet chaining is the most frequently used mode of inter-servlet communication. As the name suggests, servlet chaining involves one servlet delegating processing control to another servlet after completing its own processing. To perform servlet chaining, the calling servlet obtains the reference to the ServletContext. From the ServletContext, the calling servlet gets the reference to the RequestDispatcher object using the getRequestDispatcher() method of the ServletContext. Now, the delegation from one servlet to another servlet can be done by calling either of two methods: forward() or include().

The forward() method is the most commonly used method of the two for servlet chaining. This method accepts the HttpServletRequest and HttpServletResponse objects as parameters. The called servlet will use these objects to get the handle to read the browser's input and send output to the browser. The following code snippet illustrates this process:

 ...  // assume that the ServletContext, in this case "sc" has been obtained in // the init() method // obtain the reference to the request RequestDispatcher rd= sc.getRequestDispatcher("/OtherServlet); // forward request to the other servlet by passing the HttpServletRequest         and //HttpServletResponse objects rd.forward(requst, response) ... 

Tip

The forward() mechanism of servlet chaining should not be used if any data has been written to the output stream because the forward() method throws an IllegalStateException. When using forward(), ensure that no data has been written to the output stream by the calling servlet.


The include() method is a more flexible way to chain servlets. The include() method, similar to the forward() method, takes the HttpServletRequest and HttpServletResponse objects as parameters. A servlet called using this mechanism has limited access to and use of the HttpServletResponse object. The response HTTP headers cannot be generated by the called servlet. Here is a code snippet that illustrates this method:

 ...  // assume that the ServletContext, in this case "sc," has been obtained in the // init() method // obtain the reference to the request RequestDispatcher rd= sc.getRequestDispatcher("/OtherServlet); // call the other servlet using the include method rd.include(requst, response) ... 


Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net