|
Set Up Servlet Apps in Tomcat
Setting up servlet applications in Tomcat is as easy as 1-2-3.
by Budi Kurniawan
Posted July 16, 2002
Writing Java code for your servlet is one thing; setting up a servlet application is another. I'll show you how to set up and run your servlet application in three easy-to-follow steps. Your servlet application needs Tomcat or a similar servlet container. If you haven't installed Tomcat, read my previous article.
Step 1: Create a Directory Structure for Your Application
The install directory of Tomcat 4 is called %CATALINA_HOME%. Several subdirectories reside under this—bin, server, webapps, and so on. When setting up a servlet application, you'll use the webapps directory because it's the parent directory of each servlet/JSP application deployed with that instance of Tomcat.
For your Web application, create the application directory under webapps. For this example, call the application directory myServletApp. Within this directory, create a subdirectory named WEB-INF. Note you can create other subdirectories under the application directory to group your resources. For example, you might create separate directories for your jsp pages or image files.
Under the WEB-INF directory, create a directory called classes where you'll save your servlet class files. If your servlet is part of a package, you must also create a directory structure within classes that reflects this package name. For example, if your servlet is part of the com.brainysoftware package, create a directory under classes called com, and a directory under com called brainysoftware. When you're finished creating the appropriate directories, save your entire servlet class into the brainysoftware subdirectory (see Figure 1).
Step 2: Write and Save Your Servlet Source File
You can save your source file anywhere but the application directory or any of its subdirectories. This is because anything you save in these locations can be viewed using a Web browser. However, the WEB-INF directory is a special directory that protects your resources—you can save your source files under it if you wish.
Step 3: Compile Your Source Code
To compile your servlet, include the path to the servlet.jar file in your CLASSPATH environment variable. The servlet.jar file resides in the common\lib\ subdirectory under %CATALINA_HOME%. So, you need to edit CLASSPATH to include the path to the lib subdirectory. For example, if you installed Tomcat under the D:\ drive on Windows and you named the install directory tomcat4, type this command from the directory where your servlet source file resides (assuming your servlet source file is called MyServlet.java):
javac -classpath D:\tomcat4\common\lib\servlet.jar MyServlet.java
Back to top
|