Inter-Servlet Communication (v1.0 and 2.0 only)

This section shows how to

  • call a method of another Servlet

The inter-Servlet communication method which is described in this section can only be used with Servlet engines which implement version 1.0 or 2.0 of the Servlet API. It will not work with Servlet engines which comply strictly to version 2.1. The new way of doing inter-Servlet communication which was introduced in the 2.1 API is not done by the following method

Servlets are not alone in a Web Server. They have access to other Servlets in the same Servlet Context (usually a Servlet directory), represented by an instance of javax.servlet.ServletContext. The ServletContext is available through the ServletConfig object's getServletContext method.

A Servlet can get a list of all other Servlets in the Servlet Context by calling getServletNames on the ServletContext object. A Servlet for a known name (probably obtained through getServletNames) is returned by getServlet. Note that this method can throw a ServletException because it may need to load and initialize the requested Servlet if this was not already done.

After obtaining the reference to another Servlet that Servlet's methods can be called. Methods which are not declared in javax.servlet.Servlet but in a subclass thereof can be called by casting the returned object to the required class type.

Note that in Java the identity of a class is not only defined by the class name but also by the ClassLoader by which it was loaded. Web servers usually load each Servlet with a different class loader. This is necessary to reload Servlets on the fly because single classes cannot be replaced in the running JVM. Only a ClassLoader object with all loaded classes can be replaced.

This means that classes which are loaded by a Servlet class loader cannot be used for inter-Servlet communication. A class literal FooServlet (as used in a type cast like "FooServlet foo = (FooServlet)context.getServlet("FooServlet")") which is used in class BarServlet is different from the class literal FooServlet as used in FooServlet itself.

A way to overcome this problem is using a superclass or an interface which is loaded by the system loader and thus shared by all Servlets. In a Web Server which is written in Java those classes are usually located in the class path (as defined by the CLASSPATH environment variable).

Example. Servlet FooServlet wants to call the method public void bar() of Servlet BarServlet. Both Servlets should be reloadable so the Servlet classes cannot be loaded by the system loader. Instead we define an interface BarInterface which defines the callable method and is loaded by the system loader. BarServlet implements this interface. The Servlets are placed into the Servlet directory and the interface into a directory in the class path.

 1:  public class FooServlet extends HttpServlet
2: {
3: protected void doGet(HttpServletRequest req,
4: HttpServletResponse res)
5: throws ServletException, IOException
6: {
7: ...
8: ServletContext context = getServletConfig().getServletContext();
9: BarInterface bar = (BarInterface)context.getServlet("BarServlet");
11: ..
12: }
14: ...
15: }

 1:  public interface BarInterface
2: {
3: public void bar();
4: }

 1:  public class BarServlet extends HttpServlet implements BarInterface
2: {
3: public void bar()
4: {
5: System.err.println(""bar() called"");
6: }
8: ...
9: }