HowTos‎ > ‎

HowTo Deploy a Servlet on Tomcat

I've been working with Apache Tomcat quite a bit now. And I've seen people asking a lot of questions about little things that should be simple but aren't acutally. This particular tutorial covers deploying a servlet; step-by-step. I've written this for Tomcat 4.1  ( but I'm sure it'll work for the newer versions also ) on a Windows XP SP-2 system.

Assumptions

  • You already have Tomcat ( one version or another ) installed and working properly.
  • You've done some reading up on Tomcat and so will understand what I'm talking about in the tutorial
  • You have tried the examples that are provided with Tomcat ( http://[localhost]:[portno]/examples on 4.1 and http://[localhost]:[portno]/jsp-examples/ & http://[localhost]:[portno]/servlet-examples/ on 5.5 ) and they're working fine.


The Code

 You can download the complete webapp as a zip file (3k) from the attachments on this page, below. Unzip it and copy the servletTestApp folder into your webapps folder.


The Convention

  • Words in italics refer to specific files or folders or other objects and are case-sensitive unless mentioned otherwise.
  • Words in [square-brackets] are parameters that you'll have to replace with the appropriate values as applicable to your system.
  • $CATALINA_HOME refers to your Tomcat installation directory; it is the folder which contains your bin and webapps folders, among others.
  • Code snippets will be in this monospaced-font.


The Steps

  • Under the $CATALINA_HOME/webapps folder ( henceforth, just webapps ), create a new folder for your webapp; I'll be calling it servletTestApp.
  • Under servletTestApp create another folder called WEB-INF.
  • Under WEB-INF, create a folder called classes.
  • Also, under WEB-INF, create a file called web.xml ( just create a text file and rename the extension ).
  • Next, create the servlet, testServlet.
  • I've put the servlet in a package, testServletPackage, to demonstrate. So, create a folder under classes called testServletPackage.
  • Compile the servlet.
  • Edit the web.xml to add the following servlet mapping:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <servlet>
        <servlet-name>testServlet</servlet-name>
        <servlet-class>testServletPackage.testServlet</servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>testServlet</servlet-name>
        <url-pattern>/testServlet</url-pattern>
    </servlet-mapping>
</web-app>

  • This is to tell Tomcat which servlet is to be made available under which URL.
  • Restart Tomcat, if its already running.
  • You should now be able to access your servlet under http://[localhost]:[portno]//servletTestApp/testServlet


Notes

  • Everytime you make a change in the servlet, you'll need to restart Tomcat. This is not necessary for JSPs since Tomcat compares the timestamp of the JSP source with the compiled JSP and if it is newer, recompiles it. But servlets are not compiled by Tomcat.
  • You can put both doGet() and doPost() methods in the same servlet. And you could call the same method from each of them to give the same functionality with both methods.
  • Tomcat is case-sensitive, so type in your URL exactly as they're supposed to be. Also, it is WEB-INF not web-inf.
  • When you're using a form to submit to a servlet, in the action attribute you're supposed to specify the servlet URL. If you start with a '/' it refers to the host e.g. localhost if you're running it on your machine. '../' would refer to the folder above the current context e.g. localhost if your current location is localhost/servletTestApp.


Troubleshooting

The first place to look is in your logs under $CATALINA_HOME/logs. Make sure you go through them all to see if your webapp is mentioned there anywhere and take a note of the problem.

I cannot compile the servlet

Make sure the servlet.jar in your $CATALINA_HOME/common/lib is in your CLASSPATH environment-variable.

I get a 'HTTP Status 404'

Have you mapped your servlet properly? Make sure you haven't made any spelling mistakes and also be careful about the case.

I get a 'HTTP Status 503 - Servlet Invoker is currently unavailable'

The  invoker servlet is a built-in servlet used for allowing to user anonymous ( i.e. not mapped in your app's web.xml ). After Tomcat 4.1 ( I think ) it has been commented out by default. So if you're not mapping your servlets, you need to uncomment this. Open $CATALINA_HOME/conf/web.xml. Search for 'invoker'; you should find two instances. One should be uncommented and one would be commented out, like so:

<!--
    <servlet-mapping>
        <servlet-name>invoker</servlet-name>
        <url-pattern>/servlet/*</url-pattern>
    </servlet-mapping>
  -->

Uncomment this part ( and make sure the earlier reference was also not commented out ).

How do I access the servlet from the example without any mappings?

Considering my example to be without servlet mappings, you'd be able to access the servlet as http://[localhost]:[portno]/servletTestApp/ servlet/testServletPackage.testServlet
Comments