Sun Java System Web Server 7.0 Developer's Guide to Java Web Applications

Calling a Servlet Programmatically

First, identify which servlet to call by specifying a URI. This URI is normally a path relative to the current application. For example, if your servlet is part of an application with a context root named OfficeFrontEnd, the URL to a servlet called ShowSupplies from a browser is as follows:

http://server:port/OfficeFrontEnd/servlet/ShowSupplies?name=value

You can call this servlet programmatically from another servlet in one of two ways.


   RequestDispatcher dispatcher =
       getServletContext().getRequestDispatcher("/servlet/ShowSupplies");
    dispatcher.include(request, response);

   RequestDispatcher dispatcher =
       getServletContext().getRequestDispatcher("/servlet/ShowSupplies");
    dispatcher.forward(request, response);