Editor's note: New versions of Eclipse, Tomcat, and Java technology make some information in this article obsolete. See "Developing Web applications with Tomcat and Eclipse," written by Nathan Good in May 2007 for an update of this article.
I have been developing with Eclipse since its early days, and have found that it is one of the best things that ever happened to me for my Java development. Coming from a background of Java programming on Linux® using nothing but vi and a JDK, Java programming and debugging was a tedious task. Now, thanks to Eclipse, I can throw together quick Java-based prototypes easily. My next thought was why not integrate my JSP development into my Eclipse environment as well? Then I would be able to work more easily with my Java code and JSPs. The goal of this article is to save JSP developers time setting up Eclipse to work with Tomcat.
There are several components involved when setting up Eclipse to work with Tomcat. These components are shown in Table 1.
Table 1. Components and versions used in this article
|Java SDK 1.4.1||1.4.1_06|
|Eclipse Tomcat launcher plug-in by Sysdeo||Sysdeo Tomcat plug-in 2.2.1|
Download each of the components that you require. The sites where the files are available at the time this article was published are listed below in the Resources section.
Eclipse IDE: The Eclipse IDE is used as the development environment for your JSP pages and Java files. Eclipse is an easy IDE to use, with many features that make for quick and painless Java programming and debugging. With the addition of the Tomcat plug-in, this IDE becomes a great tool for managing entire Web projects, including HTML and JSP pages, icons, and servlets.
Java SDK: An SDK is required for the Tomcat Eclipse plug-in to function correctly. It does not necessarily have to be the Sun SDK, but it has to be an SDK, (you cannot use just a JRE as it will not work). There must be a Java compiler present in the SDK in order for Tomcat to function with Eclipse.
Tomcat: Tomcat is needed to drive your JSP pages. The Tomcat engine is a very good servlet engine that is free and easy to install.
Eclipse Tomcat Launcher plug-in by Sysdeo: This is just one of the many Eclipse plug-ins for Jakarta Tomcat. This is a good plug-in that saved me lots of time and integrated my Web projects with my Java code that I would usually use Eclipse to program.
Once you have downloaded what you need, the next step is to unzip each file. Unzip them and put them all in the same directory to keep track of what you are unzipping.
Once all of the files are unzipped, copy the Tomcat plug-in directory into the plug-ins directory in your Eclipse directory. The name of the directory that I unzipped from the Sysdeo ZIP file was com.sysdeo.eclipse.tomcat_2.2.1. I copied this entire directory into my Eclipse/plug-ins directory.
Next, install the SDK you downloaded. The Tomcat Eclipse plug-in requires that the default JRE that you set up in your Eclipse workspace is actually an SDK with a Java compiler. This is a requirement to use the Sysdeo Tomcat plug-in. Once the SDK is installed, you can start the Eclipse workbench.
Begin by starting Eclipse. You will have to configure several options from the workbench preferences pages. Open the preferences dialog by selecting Window > Preferences, as shown in Figure 1.
Figure 1. Eclipse preferences
Select the Java option from the left-hand tree view. Expand the Java element and select Installed JRE's, as shown in Figure 2.
Figure 2. JRE preference settings
Click Add and navigate to the JRE directory of the SDK you installed during the installation phase of the configuration above, as shown in Figure 3. Click OK.
Figure 3. Adding a JRE to the Eclipse preference settings
Check the check box beside the JRE you just added from the SDK you installed, as shown in Figure 4. This sets this JRE as the default JRE to be used by Eclipse. You must do this for the Tomcat plug-in to work. The Tomcat plug-in requires that an SDK be the default JRE selected in these Eclipse preferences.
Figure 4. Setting the default JRE for Eclipse and Tomcat
Next, we will set the Tomcat plug-in preferences. With the Preferences dialog still open, select Tomcat from the left tree menu, as shown in Figure 5.
Figure 5. Setting the Tomcat plug-in preferences
Select the version of Tomcat that you installed from the top radio buttons. I used Tomcat V5.0.16 so I have selected the last radio button option available, Version 5.x.
Next, we have to set the Tomcat Home variable. Next to the Tomcat Home dialog box, click Browse. Browse to the base directory of the Tomcat version that you unzipped and click OK. The Configuration file will be automatically selected and filled in for you. If you wish to select another configuration file for Tomcat instead of this one, you can browse to that file now. Otherwise the default value will work fine.
We have now completed the minimal requirements to start and run Tomcat using Eclipse and the Sysdeo Tomcat Launcher plug-in. Browse through the remaining preferences for Tomcat in the Eclipse preference dialog. You will notice that there are many other options available to you in the Tomcat preferences. For example, adding parameters to the JVM used by the Tomcat server, selecting Java projects from your workspace to add to the Tomcat classpath, as well as settings to allow the use of the Tomcat management application.
To test the integration of Tomcat and Eclipse, we will start by creating a new project. Select File > New > Project and examine the new project wizards contents. There is a new entry available in the Java section of this project wizard. Tomcat Project is a new option available (see Figure 6). Select it and click Next.
Figure 6. Creating a new Tomcat project
Give your new Tomcat project a name. For example, TomcatProject, as shown in Figure 7. Click Next.
Figure 7. Setting the Tomcat project name
Here you see that you may specify the name of the Context for your Web project and also that you may specify a subdirectory as the webapplication root. We will keep the defaults for now (see Figure 8). Click Finish.
Figure 8. Setting the Tomcat webapplication root directory
A new project with a WAR structure is created in your workspace, as shown in Figure 9.
Figure 9. Tomcat project created
The easiest way to test your installation is to create a new file in the root folder of your WAR project. Create a new file, call it "index.jsp". To do this, select your project in the workspace and right click on it. Select New > File. Name it "index.jsp" as shown in Figure 10 and click Finish.
Figure 10. Create an index.jsp file to test your configuration
Add the content shown in Listing 1 to your index.jsp file and save it.
Listing 1. Sample index.jsp
<html> <body> <%java.util.Date d = new java.util.Date();%> Todays date is <%= d.getDate()%> and this jsp page worked! </body> </html>
Now for the big moment. To start the Tomcat server, simply press on the Start Tomcat button that was added to your toolbar, as shown in Figure 11. You can also select the Tomcat menu from the main menu bar and select Start Tomcat.
Figure 11. Start the Tomcat server using the Sysdeo Tomcat plug-in
The Tomcat server will start and its startup text will appear in the Eclipse workbench's Console view, as shown in Figure 12. Examine the startup log and watch for any errors.
Figure 12. Tomcat's startup information is displayed in the Eclipse Console view
When the server has started, launch a Web browser. Go to http://localhost:8080/TomcatProject. A page should load and you should get a message similar to this one:
Todays date is 30 and this jsp page worked! (The 30 appears on my screen because today is the 30th)
You should now have Eclipse set up and configured to work with Jakarta Tomcat. You now have the power to develop and test quick JSP's with integrated Java code, using a great IDE that will help improve your productivity. Using Eclipse to program Java code and having it integrated with Jakarta Tomcat will make JSP development fun and easy.
- New versions of Eclipse, Tomcat, and Java technology make some information in this article obsolete. See "Developing Web applications with Tomcat and Eclipse," by Nathan Good in May 2007 for an update of this article.
"What is Eclipse and how do I use it?" provides a lot of information for users new to Eclipse.
The Jakarta Tomcat Web site contains a plethora of information about Tomcat.
Expand your Eclipse skills by visiting IBM developerWorks' Eclipse project resources.
Browse all of the Eclipse content on developerWorks.
Visit the developerWorks Open source zone for extensive how-to information, tools, and project updates to help you develop with open source technologies and use them with IBM's products.
Stay current with developerWorks technical events and webcasts.
Get products and technologies
Download Eclipse from Eclipse.org.
Download Tomcat from the Jakarta download page.
Download the Java SDK V1.4.1 from the Sun Web site.
Download the Sysdeo Tomcat plug-in from the Sysdeo Web site.
See the latest Eclipse technology downloads at IBM alphaWorks.
Innovate your next open source development project with
IBM trial software, available for download or on DVD.
Get involved in the developerWorks community by participating in developerWorks blogs.