Servlet Development Guide : PART II Administration : Chapter 4 Configuring Objects in the Servlet Container : Define and Map Servlets
Define and Map Servlets
A web.xml file must first define a servlet instance, then map that instance to one or more URL patterns. For details on programming servlets, see Chapter 9 .
Defining the Servlet Instance
The servlet element of a web.xml file defines a servlet instance. The servlet element always contains a servlet-name element and a servlet-class element, and may also contain initialization parameters.
 
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>rwsf_servlet_example.createHelloWorldExample</servlet-class>
</servlet>
The servlet-name element declares a name for this particular servlet instance. Each servlet instance in a context must have a unique name. However, the name is only used to associate URL mappings with this instance, and need not correspond to the name of the servlet class or the URL of the servlet.
The servlet-class element tells the container how to construct an instance of the servlet class. There are two components to this element. The element contains the base name of the shared library (.dll or .so) that contains the servlet, and the name of the function that the container uses to load the servlet. The components of the element are separated by a single period. For example, the element status.createStatusServlet contains the base name status and the function name createStatusServlet.
The base name of a shared library is the name of the library without the platform-specific prefix (if any), without the 12d or 15d suffix, and without the platform-specific extension. See Appendix C for information on library name mappings for each platform that HydraExpress supports.
The function name in the servlet-class element must match the function name defined in the servlet implementation. The RWSF_DEFINE_SERVLET macro in the implementation source code defines the function name, as described in “Defining Filters, Servlets, and Listeners” . Note that the name of this function must be a legal C++ identifier.
The servlet element below defines a servlet named HelloWorld. The servlet shared library has the base name rwsf_servlet_example, and the container calls the function createHelloWorld to load the servlet instance. Notice that the name of the servlet instance does not need to correspond to the class name.
 
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>rwsf_servlet_example.createHelloWorldExample</servlet-class>
</servlet>
To add another instance of the same servlet class to the container, use another servlet element with a different servlet-name. The web.xml file fragment below defines two different instances of the same servlet class:
 
<servlet>
<servlet-name>getStatus</servlet-name>
<servlet-class>status.createStatusServlet</servlet-class>
</servlet>
 
<servlet>
<servlet-name>getFullStatus</servlet-name>
<servlet-class>status.createStatusServlet</servlet-class>
</servlet>
Each servlet element may contain any number of init-param elements. The container passes the parameters to the servlet in much the same way that command line parameters are passed to a program, so the exact parameter names and values required depend on the individual servlet. (See below for an example of the corresponding servlet code.) The example below shows a web.xml file fragment that defines two different instances of the same servlet class.
The container starts each instance with different parameters.
 
<servlet>
<servlet-name>getStatus</servlet-name>
<servlet-class>status.createStatusServlet</servlet-class>
<init-param>
<param-name>output</param-name>
<param-value>brief</param-value>
</init-param>
</servlet>
 
<servlet>
<servlet-name>getFullStatus</servlet-name>
<servlet-class>status.createStatusServlet</servlet-class>
<init-param>
<param-name>output</param-name>
<param-value>verbose</param-value>
</init-param>
</servlet>
Here is an example of the servlet code that might retrieve the initiation parameter.
 
rwsf::ServletConfig config = getServletConfig();
rwsf::ServletContext context = config.getServletContext();
std::string initParam = context.getInitParameter("output");
A servlet element only defines an instance of a servlet. For the container to forward requests to the servlet, the servlet must be mapped to one or more URLs or called by name from another servlet or a filter.
Mapping Servlets to URLs
The servlet-mapping element associates a URL pattern to a servlet instance. The servlet-mapping always contains a servlet-name element and a url-pattern element.
The servlet-name element must match a servlet-name defined in a servlet element elsewhere in the web.xml file. Since a servlet container may have multiple instances of the same servlet running, the container uses the servlet-name to associate a mapping with a servlet.
The url-pattern element contains a pattern that the container uses to determine whether an incoming request should be forwarded to a given servlet. In simple terms, the url-pattern determines the location of the servlet within the context. The url-pattern element can also contain more sophisticated mappings, as described in “URL Patterns.”
For example, the web.xml fragment below maps the URL /status/* to a servlet named getStatus. If the servlet is defined in the examples context of a container at http://example.com/, then this element tells the container that the getStatus servlet should receive requests for URLs that start with http://example.com/examples/status.
 
<servlet-mapping>
<servlet-name>getStatus</servlet-name>
<url-pattern>/status/*</url-pattern>
</servlet-mapping>
A servlet may receive requests from any number of URL patterns. The fragment below maps two different patterns to the same servlet instance:
 
<servlet-mapping>
<servlet-name>getStatus</servlet-name>
<url-pattern>/status/*</url-pattern>
</servlet-mapping>
 
<servlet-mapping>
<servlet-name>getStatus</servlet-name>
<url-pattern>/briefStatus/*</url-pattern>
</servlet-mapping>
Although different patterns can map to the same servlet, the same pattern cannot map to two different servlets. If the same url-pattern appears more than once within a web.xml file, the HydraExpress Agent makes no guarantees about which servlet receives a request. “URL Patterns” describes the url-pattern element in detail.
Single-thread Access
Servlets are inherently multithreaded. However, the servlet container supports a simple mechanism for serializing access to a particular servlet. If a servlet element contains the attribute single-thread="true", the container only allows one thread at a time to enter the servlet. If a new request arrives for the servlet while a thread is active in the servlet, the new request waits until the active thread leaves the servlet.
The servlet element below specifies that only one thread at a time may enter the servletInfo instance.
 
<servlet single-thread="true">
<servlet-name>servletInfo</servlet-name>
<servlet-class>rwsf_servlet_example.createServletInfo</servlet-class>
...
</servlet>
For more information on the implications of single-thread servlets, and how to address performance issues, see “Single-Thread and Multithread Issues,” in Chapter 3 of the HydraExpress User Guide.