Tomcat Setup - Running your first Servlet
This is a short reference on how to install TOMCAT 5 on Windows XP, set up the initial server settings in server.xml and run your first servlet from your context, by modifying your context's web.xml file. Your context is the folder where you will store your web application files like servlets, jsp, htmls, images and other files.

Tomcat 5 INSTALLATION (to run jsp and servlets)

Since I had my apache server (web server) running on the 80 port, TOMCAT needed to be installed to listen in a different port. I chose the port 8080.

The first step is to download the tomcat server installation file by going to jakarta.apache.org Download the windows binaries and install it. I installed it at c:\tomcat\tomcat5, and i will refer to that path as $TOMCAT_HOME through the rest of this tutorial.

Make sure you also have Java installed (JDK) in your system. The environment variable $JAVA_HOME should be set already in the windows environment variables. If you don't have JAVA installed go an download the JDK from the Sun Download page

The main configuration files for Tomcat are:

1. $TOMCAT_HOME/conf/sever.xml

2. $TOMCAT_HOME/conf/web.xml ( will have the default setup for all the contexts)


I will assume Java is installed in the folder $TOMCAT_HOME from now on in this tutorial.
Copy the $JAVA_HOME/lib/tools.jar to $TOMCAT_HOME/common/lib so that TOMCAT is able to compile jsp pages into servlets. But you can also avoid this step by setting the JAVA_HOME environment variable properly in windows.

To continue Tomcat must be running now, by going to http://localhost:8080/

TOMCAT CONTEXTS

A context is a 'folder structure' where you will store your web application files files (html, jsp, servlets, images, flash, etc...). You can have a different context for every web application that is running on your server. The contexts are stored in $TOMCAT_HOME/webapps/

For instance, if i am working in 2 web applications called "store1" and "messageboard" then i can crate 2 folder with the same name inside $TOMCAT_HOME/webapps/. Just be sure to follow the following conventions when creating a context:

Let's say i want to create a project called store1, then i would have the following folder structure at $TOMCAT_HOME/webapps/:

$TOMCAT_HOME/webapps/
          store1/
                WEB-INF/
                            src/
                            classes/
                            lib/
                            web.xml

 

- Basically, web.xml is the configuration file of our context or web application.
- IN src we will have our java source files (organized by packages), which are our servlets source code. But we will only have the .java files, not the .class files there.
- In the folder store1 we can put our jsp pages or html pages
- In lib we will have jars (specific for this project), like database jdbc drivers and other vendors' libraries.
- In classes we will have the .class (compiled java files) which are our servlets.


Configuring the web.xml for our context

Finally, we need to modify the web.xml for our context. We can use the $TOMCAT_HOME/conf/web.xml if we want all of our contexts to share the same configuration, but because every web project is different, i would use: $TOMCAT_HOME/webapps/store1/WEB-INF/web.xml for my above example.

1. First, i will turn on the automatic reload, so if i change a JSP i don't have to restart the tomcat server every time but the server will detect this change automatically and compile properly. Turn it off when the site goes live, to improve efficiency. To make this change open $TOMCAT_HOME/conf/web.xml and near the end, before </host> it should say:

     <DefaultContext reloadable="true"/>

2. Now, in $TOMCAT_HOME/webapps/store1/WEB-INF/web.xml, that is, that web.xml file of our context we need to add a servlet mapping property like this:,

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

so you can access your servlets using http://localhost/store1/servlet/helloworld, where project1 is your context and your first servlet "hello world" is located at:
$TOMCAT_HOME/webapps/store1/WEB-INF/
classes/helloworld.class

The above configuration allows you to invoke any servlet that is in the "classes" folder of your web application. But let's say you want to invoke only certain servlets and restrict access to the others. Then you would need to avoid using the above method and declare only the servlet that will be accessible from the web.

Let's say we only want the servelt helloworld to be accessible, then in the web.xml file
in ( $TOMCAT_HOME/webapps/store1/WEB-INF/web.xml ) do the following:

A) add to web.xml first this servlet declaration statements:

<servlet>
    <servlet-name>helloworld</servlet-name>
    <description>This is my first servlet</description>
    <servlet-class>helloworld</servlet-class>
    <load-on-startup>5</load-on-startup>
</servlet>

B) then add a mapping for this servlet like this, in the same web.xml:

<servlet-mapping>
      <servlet-name>helloworld</servlet-name>
      <url-pattern>/helloworld</url-pattern>
</servlet-mapping>


At this point you will be able to access your servlet helloworld as follows:

http://localhost:8080/store1/helloworld

DOWNLOAD HellowWorldCode

Here is the example for helloword. Download here


Compiling your java files

To compile my projects i use Borland JBuilder, but many people use Ant which can generate jar, war and class files. Ant is very useful but i am not covering it in this tutorial. For more information go to this link:
(http://jakarta.apache.org/ant/manual/) Ant is like a unix make tool to compile your java files and do more things like producing WAR files and CLASS files in predetermined folders.

JBorland is free, the only thing i had to configure on the IDE was the addition of the following library reference:

- $TOMCAT_HOME/common/lib/servlet-api.jar

so JBuilder would be able to compile servlets (by accessing the javax.* package)

Jbuilder configURATION TO cOMPILE THE SERVLET

For instance, i created a new JBuilder project for my example web application (store1) and set the following info:

- $TOMCAT_HOME/store1/WEB-INF/src will contain my source code

- $TOMCAT_HOME /store1/WEB-INF/classes will contain my compiled .class files

That way i maintain my source code (.java files) and my compiled classes (.class) separated.

Each you modify your servlet you can either compile using JBuilder, and if you have the reload option set to ON in server.xml reload you don't have to restart tomcat. But if you don't see the changes just restart it :(

As Always, i would recommend the use of some CVS application for version control and backup of your code.