Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Tomcat on Linux tutorial  RSS feed

Jeff Salter
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure this has been done before, but why not one more? In my initial forays into writing, compiling, deploying, running servlets on Tomcat running on Linux, I encountered extreme frustration. Frankly, the documentation/tutorials out there, including the ones at the Tomcat website, the ones at the Sun website, and others, are lacking. They tell you how to write a servlet. They tell you to create a WAR. They tell you to deploy the WAR to the CATALINA_HOME directory, etc etc.

However, the tutorials often severely lack a lot of the necessary details, and I would always run into stumbling blocks. So I would Google this, Google that, paruse message boards, and fight with it until I would finally come up with a solution.

Sure, I was always able to easily do servlets with Tomcat using NetBeans, or some other great tool. But I always ran into problems trying to do it all manually in the commnand line (a worthwhile endevour, to understand how everything works and is put together properly, rather than relying on the IDE crutch).

So, here is how it all works (at least for me, with a Debian install and Tomcat5, and Java 1.5 (as well as gcj):

1. Create Web Application directory:
NameOfProjectDirectory/WEB-INF/classes (optional sub directories like sample here)
NameOfProjectDirectory/WEB-INF/lib (optional, if needed)

2. In NameOfProjectDirectory/WEB-INF/classes (/subdir) Compile:
javac -cp /usr/share/tomcat5/common/lib/servlet-api.jar

the -cp argument is, of course, providing the classpath to the servlet jar file. Without providing this, the servlet won't compile. You could, of course, set the CLASSPATH environment variable as well.

3. In NameOfProjectDirectory (root) Make WAR file:
jar -cvf NameOfWarFile.war .
(don't forget the ".", with a space between it and the war - it's important)

4. Stop Tomcat (if already running)
/etc/init.d/tomcat5 stop

Here, you log on as root, ("su"), and it will prompt you for root's password. Root access is necessary to stop/start Tomcat, and is necessary to copy the WAR into the webapps directory of Tomcat.

5. copy WAR file to webapps directory of Tomcat:
cp NameOfWarFile.war /usr/share/tomcat5/webapps

Here, cp is the the Linux copy command, not to be confused with the -cp argument to javac.

6. Start Tomcat:
/etc/init.d/tomcat5 start

7. In browser:
http://localhost:8180/NameOfWAR/NameOfServlet (or how it's named in URL pattern tag of web.xml)

In my Tomcat install on my Debian/Kanotix install, Tomcat is configured to port 8180. With most Tomcat installs, it defaults to 8080.

Servlet sample code (

package test;


import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
PrintWriter out = res.getWriter();

out.println("<h1>Hello, world!</h1><br>");
out.println("<h1>Jeff loves Servlets!</h1>");

web.xml Sample code:

<web-app xmlns="" version="2.4"
<!-- in this example, the servelt is in a subdir/package called test -->

<!-- accessed in browser, or HTML/JSP link, via "/NameOfWar/hello" -->

If one wants to add JSPs or html forms (that call the servlet), it's a matter of having them in the root of your project directory (above WEB-INF). If there are additional classes that the servlet(s) depends on or accesses, put them in NameOfProjectDirectory/WEB-INF/lib

Also, there is an easier way of deploying the servlet (leaving the command line a bit). It's in the Tomcat Manager page (accessed, of course, in your browser). First, you have to add a user account in the tomcat-users.xml file (the xml there is pretty self explanatory, you just have to make it an administrator). Then you have to bring up the Tomcat homepage in your browser - http://localhost:8180/ - then you click on the Tomcat Manager Link on the left, and you'll be prompted to enter your user id and password. After than, in the Manager page, scroll down to the deploy war file option, and click on the browse button to browse to where you created the WAR. Then click on "Deploy". Then it's ready to go. No loggin on as root in the command line, not stopping Tomcat, no copying the WAR to the webapps of Tomcat directory, not restarting Tomcat.

Then, to be even easier, one can create an Ant script that does everything automatically. But that's the subject of another tutorial. ;-)

Finally, it's brain-dead easy to create web apps in NetBeans. But it's very very very worthwhile to do it all in the command line, in order to learn how everything works and is put together.

I hope this helps for any Tomcat/Servlet newbies out there!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!