Java Reference
In-Depth Information
application here and through the rest of the chapter will be shown as MyWebApp ,
but this can be any name of your own choosing.
1. Place HTML fi les and JSPs (covered in the next chapter) within MyWebApp .
2. Place servlets within MyWebApp\WEB-INF . If packages are used, there must be
a folder structure within classes to refl ect this.
3. Create a fi le called web.xml within WEB-INF . This fi le is known as the
deployment descriptor and specifi es details of the Web application (as described
below). In particular, it must contain <servlet> and <servlet-mapping> tags for
each servlet.
The opening lines of the deployment descriptor are always the same and may
simply be copied from one Web application to the next. In fact, it is highly advisable
to copy these lines (i.e., via a wordprocessor, not by transcribing them), since it is
very easy to make a mistake, particularly with the string identifying the XML
schema location. The naive user may even place a line break in the middle of this.
The example below shows a deployment descriptor for a Web application
containing a single servlet called FirstServlet . The servlet must have <servlet> and
<servlet-mapping> tags that identify the associated Java .class fi le and the servlet's
URL location (relative to the web application) respectively. These <servlet> and
<servlet-mapping> tags will have exactly the same structure for any other servlet.
Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<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">
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>
</web-app>
Note the use of a '/' in the <url-pattern> tag! This is easily omitted.
If any changes are made to servlet tags after Tomcat has started, it will be neces-
sary to stop Tomcat (via shutdown ) and re-start it (via startup ). This is also
necessary after changing any servlet.
Search WWH ::




Custom Search