|
|
|
|
|
|
|
|
Defining Projects in your Apache/Tomcat Intranet Environment Page III
P.G. Daly Go to page: 1 2 3 04/17/02
Once you have these configurations intact, you need to
perform servlet and URL mapping for each project so Tomcat knows how to relate
what the user types in the browser to the contexts defined above. This part of the process is identical for
JServ and mod_jk. Each web application (that is, each subdirectory under
%Tomcat_Home%/webapps should have its own WEB-INF and META-INF
subdirectories. In the WEB-INF
subdirectory there is a file called web.xml.
This is the file that defines the servlet and URL mapping for your
application. The relevant parts of
web.xml will look as follows for our application: <?xml version="1.0"
encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <servlet>
<servlet-name> snoop
</servlet-name>
<servlet-class>
SnoopServlet
</servlet-class> </servlet> <servlet>
<servlet-name> HelloWorld
</servlet-name>
<servlet-class>
HelloWorldExample
</servlet-class> </servlet>
<servlet-mapping> <servlet-name> snoop
</servlet-name>
<url-pattern> /snoop
</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>
HelloWorld
</servlet-name> <url-pattern> /hello
</url-pattern>
</servlet-mapping> </web-app> Without getting into an XML lesson, each application has a <servlet></servlet>
element which maps the actual class name to the name you want to call the
servlet and a
<servlet-mapping></servlet-mapping> element which maps the
servlet name you defined in <servlet> to a URL the user will type in. Let us deconstruct the Snoop example. Earlier, we defined the mount point as
follows: # This mount point will be used for examples ApJServMount /examples
/root This means that anything with http://localhost/examples will
map to the root application, which is not very meaningful until we define
servlets that correspond to this. That
is what we accomplish with the following code: <servlet>
<servlet-name> snoop
</servlet-name>
<servlet-class>
SnoopServlet
</servlet-class> The above code means that the servlet name of
"snoop" corresponds to a class by the name of
"SnoopServlet". Remember that
this class is located within the examples web application
(%Tomcat_Home/wepapps/examples/WEB-INF/classes) because we are editing the
web.xml within the /examples/WEB-INF subdirectory. Next, we define the URL mapping to the servlet as follows: <servlet-mapping>
<servlet-name> snoop
</servlet-name>
<url-pattern> /snoop
</url-pattern>
</servlet-mapping> This means that if a user types in /snoop after http://localhost/examples (that is, http://localhost/examples/snoop) it
will call the servlet named "snoop" which we know from a paragraph
ago corresponds to the SnoopServlet class. Take a moment and breathe.
At least for me, I know it is a lot to get your head around and it can
be very confusing. However, once you
work with it more, you begin to understand how the items within web.xml for
each application relate not only to each other but also to the mounting points
and the environment as a whole. If you
take these examples and begin to expand upon them in your own environment, it
will help you to form a fully functioning, multi-project environment one step
at a time. |
| |
|
· Intranet eXchange Discussion Board |
Intranet Journal's Tutorials |
|
Managing Editor |