How to add a Servlet to your Web Application in Tomcat

1. Upload compiled servlet

Your compiled servlets should go unpacked into WEB-INF/classes/package/dirs/ CompiledServlet.class . e.g. The intranet.AdminControllerServlet should go inside WEB-INF/classes/intranet/AdminControllerServlet.class

If you like, you can package your Servlets into a JAR library file, and stick the JAR file in WEB-INF/lib/

2. Locate web.xml

The web.xml should be located inside theWEB-INF/. It is the same file that was used when you specified the security constraints for your web application.

3. Add servlet and servlet mappings

Before the security constraint elements, and underneath the web-app, add all your servlets.

Then add all your servlet mappings, the servlets and servlet mappings cannot be mixed together, because this will break the order defined in the dtd.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    <servlet-name>Admin Controller</servlet-name>
    <servlet-name>Timesheet Controller</servlet-name>
    <servlet-name>Admin Controller</servlet-name>
    <servlet-name>Timesheet Controller</servlet-name>

The url-patterns are all relative to http://tomcatserver/Context_Path/, where Context_Path is defined as part of the server.xml configuration file. See How to add a Web Application in Tomcat for more details. This sample configuration will make browsers accessing the URL http://tomcatserver/ Context_Path/admin/ run the intranet.AdminControllerServlet servlet. Likewise the URL http://tomcatserver/Context_Path/timesheet/ will run the intranet.TimesheetControllerServlet servlet.

4. Links

The web.xml dtd can help you determine which order elements are meant to go in the web.xml file.