CHAPTER 2: Building Web Applications Using Servlets and JSP 59
Examining the Request
When the client (web browser) makes a request (a GET request in this case), the web server (Tomcat)
sees the resource path /helloworld/hello in the request in line 1 and determines that the resource
requested by the user is not a static page (for example a .html file) and so forwards the request to
the web container (Tomcat). Astute readers will notice that Tomcat serves the role of the web server
and the web container.
Locating the Servlet
The resource path in the request (line 1 in Listing 2-3) is mapped to the HelloWorld servlet through
the web.xml file written in Listing 2-2. This web.xml file is called a deployment descriptor because it
describes the deployed servlet to the web container. Through the deployment descriptor, the web
container determines the servlet that needs to be called to serve the original HTTP request that the
web browser initiated. For the sake of reader’s convenience, the web.xml is shown again here in
Listing 2-4.
Listing 2-4. web.xml
1.<?xml version="1.0" encoding="UTF-8"?>
2.<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
3.
4.
5.
6.apress.helloworld.HelloWorld
7.
8.
9.
10.
11.
12.
13.
Lines 1 to 2: These lines contain boilerplate XML stating the version, encoding,
and schema used for the XML file.
Lines 3 to 8: The <servlet> tag is used to configure our servlet. It contains
two nested tags: <servlet-name> defines a logical name for the servlet, and
<servlet-class> indicates the Java class defining the servlet.
Lines 9 to 12: The <servlet-mapping> XML tag is used to configure our servlet.
It contains two nested tags: <servlet-name> matches the value set in the
<servlet> tag, and <url-pattern> sets the URL pattern for which the servlet
will execute.
Java EE web applications run from within a context root. The context root is the first string in the URL
after the server name and port. For example, in the URL http://localhost:8080/ helloworld/hello,
the string helloworld is the context root. The value for