Installing Tomcat

Before integrating Tomcat with Eclipse, you need to download and install Tomcat. There are several ways to get Tomcat up and running. On one end of the spectrum, it is possible to download Tomcat's source code and build and test it. On the other end, you can simply download a binary version of it. Here, we are taking the arguably easiest path to installing Tomcat: downloading, configuring, and running the compiled binary. Throughout this chapter, we assume you are using the most recent release of Tomcat. As of the time of this writing, the latest version is 7.0.

Tip

It is also possible to install Tomcat by getting Eclipse to download it for you. Provided that you already have WTP installed (how to install this plugin is described later on), when you create a new server under Eclipse and set its type as Apache Tomcat, you can use the Download and Install button on the server definition wizard to install Tomcat. You can even select in which folder you want to install the server container.

You can download the binary version of the latest version at http://tomcat.apache.org/download-70.cgi. After being downloaded, Tomcat can be installed by simply unzipping it, which creates the following directory hierarchy:

Installing Tomcat

Arguably, the most important directory is the webapps directory. This is the deployment directory, and files that need to be made available to client browsers, JAR files, configuration files, and so on are placed into it. Throughout this chapter we are going to provide details on how to organize web applications under this directory.

To set up and run Tomcat outside Eclipse, you need to configure two environment variables, namely, JAVA_HOME and CATALINA_HOME. The JAVA_HOME environment variable should be set to the base path of your JDK. Tomcat 7.0 requires Java SE 6.0 or later. CATALINA_HOME should point to the base path of the Tomcat directory hierarchy. Instructions on how to set these environment variables for all operating systems can be found at http://tomcat.apache.org/tomcat-7.0-doc/setup.html. In addition, the file RUNNING.txt in the Tomcat's root directory also contains installation instructions.

Tip

It is worth mentioning that, although we use Tomcat throughout this chapter, the examples shown here are not Tomcat-specific. Therefore, you can deploy and run all examples shown here in other servlet containers.

At first we are going to execute Tomcat outside Eclipse, later in this chapter we are going to explain how to execute it inside Eclipse. After setting the aforementioned environment variables (following the instructions for your operating system, as described in the URL provided previously), you need to change to Tomcat's bin directory. From there, you just need to type startup if you are a Windows user or startup.sh if you are a Unix-based operating system user. If everything went well, Tomcat has started. To verify if Tomcat is up and running, open a browser and type http://localhost:8080. You should see Tomcat's welcome page, as shown in the following screenshot. A few words about this URL are in order. First, localhost is what you use to access local web servers. This part of the URL resolves to the following IP address: 127.0.0.1. As for the 8080 part, this corresponds to the port number. Usually, web servers use port 80. Tomcat, however, is configured to listen on port 8080 to avoid possible conflicts with other web servers.

Installing Tomcat

Tip

In some operating systems, for instance, Ubuntu, Tomcat is started automatically. Thus, whenever you want to stop and start Tomcat, use the scripts stop and restart in the bin directory.

Tomcat ships with a web-based administration console. This application is named Manager Webapp, and it starts automatically along with Tomcat. However, you need to do some configuring before being able to use it. In order to be allowed to log in and use the Manager Webapp, you must first configure the file tomcat-users.xml, which is in the conf directory. Edit this file by adding the following lines:

<tomcat-users>
   <role rolename="manager-gui"/>
   <user username="tomcat" password="tomcat" roles="manager-gui"/>
</tomcat-users>

Remember that after modifying Tomcat's configuration files, the changes do not take place right away. To indicate Tomcat that something has changed you need to stop the server and then restart it. Upon restarting Tomcat, go to the following URL to access the application:

http://localhost:8080/manager/html

The HTML interface of the Manager Webapp is shown in the following screenshot. As you can see, it lists all deployed web applications. The Manager allows you to install, reload, undeploy, and stop web applications. If you want to learn how to perform these operations using the Manager Webapp, then check the accompanying documentation.

Installing Tomcat
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset