Configuring Eclipse to use Tomcat and the Spring Framework

There never seems to be enough documentation on how to create Spring Framework enabled projects in Eclipse. Spring is widely used for web applications so you need to know how to integrate Tomcat and Eclipse as well – so there is even less documentation! There follows below a step-by-step set of instructions to do just that.

I am not going to describe how to install the Java Runtime environment. If you need help with that, then maybe web services is not the career you should be focusing on 😉 Needless to say it is a requirement of all the following technologies.

Installation
Windows is (not) funny about spaces in directory names so all the installations will be located under c:\java instead of c:\Program Files. You’ll save yourselves an enormous headache trying to figure out why Windows is not interpreting the command lines properly if you just remove the cause of the problem. Bloody Bill!

  1. Download Eclipse IDE for Java EE Developers 3.4 Ganymede from the downloads section of the Eclipse web site.
  2. Extract the eclipse-jee-ganymede-win32.zip into c:\Java\eclipse.
  3. Create a desktop shortcut to the Eclipse executable, for easy launching.
  4. Download Tomcat 6.x from the downloads section of the Tomcat web site. You will need to get the base (Core) pack and not the windows installer pack. The windows installer pack does not include some of the command-line scripts for launching Tomcat.
  5. Extract the apache-tomcat-6.0.18.zip into c:\java\apache-tomcat-6.0.18 then rename it to c:\java\tomcat.
  6. Launch Eclipse.
  7. On a new install of Eclipse it asks you where you want to put your workspace. We are going to place it in c:\java\workspace because the spaces issue will be a problem if it goes into c:\Documents and Settings\DavidQ\workspace.
  8. Next we are going to install our Spring IDE plugin, full instructions on how to do this are on my blog page entitled Eclipse 3.4 Ganymede and Spring IDE 2.0.6.
  9. After installation it says It is strongly recommended you restart your system for the changes to take effect. .. Restart now?, when it says system it means the eclipse workbench and not your whole computer, so click Yes.

Configure Tomcat inside Eclipse
Next we need to tell Eclipse about which Tomcat we are using and set up the integration.

  1. From the menu bar select Window->Preferences.
  2. The Preferences window will open. Expand Server.
  3. Highlight Runtime Environment. This is the place where you can tell Eclipse about where you put your Tomcat installation.
  4. Click Add.
  5. Expand Apache and select Apache Tomcat v6.0.
  6. Check the Create a new local server check box to create a new server in the Servers view.
  7. Click Next.
  8. Leave the Name of the server set to Apache Tomcat v6.0.
  9. Eclipse gives the option to download a version of Tomcat but it usually does not have the latest version (which is why we downloaded it separately). Click Browse and navigate to c:\java\tomcat and click Ok.
  10. Leave the JRE drop down set to Workbench default JRE.
  11. Click Finish.
  12. You are taken back to the Server Runtime Environments preference page so click Ok.
  13. A new project will have been created and if you go into the Servers view (in the bottom set of tabs), you will see a new server Apache Tomcat v6.0 at localhost.

Create project
There are several different types of project in Eclipse and we need the one that will allow us to integrate all the things we need.

  1. Select File->New->Dynamic Web Project
  2. Enter a Project name. Although you can have spaces, in my opinion, it’s best not to put any in. The project name becomes the directory name and for the same reason as above it’s a pain if there are spaces.
  3. The Target Runtime should already be set to Apache Tomcat v6.0.
  4. Also keep the Configuration set to Default Configuration for Apache Tomcat v6.0.
  5. Click Finish.
  6. In the Project Explorer, right click on the new project and select Spring Tools->Add Spring Project Nature.

Spring Web project and Tomcat project association
We must now associate the project with Tomcat.

  1. Make sure the Servers view is visible (in the bottom set of tabs). Highlight your new Spring Web project and drag it on to the Tomcat v6.0 Server at localhost entry in the Servers tab.
  2. We want to use the Tomcat server when we use our new project, so we can set the Server project to be a dependency of our new project. This will mean that when we open our project the Server project will open automatically.
  3. Right click on our project and select Properties.
  4. Select the Project References option and check the Servers project.
  5. Click Ok.