Sun Java System Web Server 6.1 SP7 Programmer's Guide to Web Applications

Calling a Servlet Programmatically

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

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

You can call this servlet programmatically from another servlet in one of two ways, as described below.


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

         

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