Do I always have to package my servlets in WARs with tomcat? I have a lot of html files that refer to my servlets with the following style of URL: "http:localhost:8080/servlet/myservlet", which worked fine with Java WebServer - Is there any way I can configure tomcat to allow for this type of deployment?

Tomcat is a J2EE compliant webserver and the correct way to deploy servlets is packaged in a WAR file.

With JWS, files located in the public_html folder were automatically served by JWS. Even though tomcat does not have such a concept it is possible to provide similar behavior.

Tomcat expands any deployed WAR files into a very specific directory tree (as specified by the Servlet 2.2 specification) under a folder whose name matches that of the war file. Typically, to invoke a servlet deployed in a web application, the following URL would be used:

http://localhost:8080/web_application_name/servlet_name

Of course, in our case, we don't want to use an web application - in such cases, we need to make the servlet 'global' to the web container. Tomcat provides a ROOT context that is used to host any servlets and jsp pages that are 'global' to the web-container i.e. not contained in a web application. Tomcat also provides a 'backwards compatable' URL pattern mapping that permits the JWS style of referring to serlvets (e.g. http://localhost:8080/servlet/myservlet).

To deploy a 'standalone' servlet, do the following:

  • Locate the ROOT web application folder. Typically this would be in /\var\servers\\wars\tomcat3.
  • Copy your compiled servlet classes to ROOT\WEB-INF\classes folder. If your servlets are in a jar file, then copy the jar file to ROOT\WEB-INF\lib
  • Restart the tomcat service.

You should now be able to access the servlets by a url of the form "http://localhost:8080/servlet/myservlet"