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

Invoking Servlets

You can invoke a servlet by directly addressing it from a Web page with a URL or by calling it programmatically from an already running servlet.

Calling a Servlet With a URL

You can call servlets by using URLs embedded as links in HTML or JSP pages. The format of these URLs is as follows:


The following table describes each URL section.

Table 4–1 URL Fields for Servlets Within a Web Application

URL Element  



The IP address or host name and optional port number. 

To access the default web application for a virtual server, specify only this URL section. You do not need to specify the context_root or servlet_name unless you also wish to specify name-value parameters.


The context path without the leading “/” at which the web application is installed. 


Only needed if no servlet-mapping is defined in the web.xml file.


The servlet-name (or servlet-mapping if defined) as configured in the web.xml file.


Optional servlet name-value parameters. 

In this example, example is the host name, MortPages is the context root, and calcMortgage is the servlet name:

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:


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

   RequestDispatcher dispatcher =
    dispatcher.include(request, response);

   RequestDispatcher dispatcher =
    dispatcher.forward(request, response);