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 NameOfProjectDirectory/WEB-INF 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 NameOfServlet.java
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) su /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: su 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.
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:/java.sun.com/dtd/web-app_2_3.dtd"> <servlet> <servlet-name>hello</servlet-name> <!-- in this example, the servelt is in a subdir/package called test --> <servlet-class>test.HelloServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>hello</servlet-name> <!-- accessed in browser, or HTML/JSP link, via "/NameOfWar/hello" --> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
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!
Hey, sticks and stones baby. And maybe a wee mention of my stuff: