Configuring Tomcat in Eclipse

We will perform the following steps to configure Tomcat in Eclipse:

  1. In the Java EE perspective of Eclipse, you will find the Servers tab at the bottom. Since no server is added yet, you will see a link in the tab as shown in the following screenshot—No servers are available. Click this link to create a new server...

Figure 2.1: The Servers tab in Eclipse JEE

  1. Click the link in the Servers tab to add a new server.
  2. Expand the Apache group and select the Tomcat version that you have already installed. If Eclipse and the Tomcat server are on the same machine, then leave Server's host name as localhost. Otherwise, enter hostname or IP address of the Tomcat server. Click Next:

Figure 2.2: Selecting a server in the New Server wizard

  1. Click the Browse... button and select the folder where Tomcat is installed.
  1. Click Next until you complete the wizard. At the end of it, you will see the Tomcat server added to the Servers view. If Tomcat is not already started, you will see the status as stopped.

Figure 2.3: Configuring Tomcat folder in the New Server wizard

  1. To start the server, right-click on the server and select Start. You can also start the server by clicking the Start button in the toolbar of the Server view.

Figure 2.4: The Tomcat server added to the Servers view

Once the server is started successfully, you will see the status changed to Started. If you click on the Console tab, you will see console messages that the Tomcat server outputs during startup.

If you expand the Servers group in the Project Explorer view, you will see the Tomcat server that you just added. Expand the Tomcat server node to view configuration files. This is an easy way to edit the Tomcat configuration so that you don't have to go look for the configuration files in the filesystem.

Double-click server.xml to open it in the XML editor. You get the Design view as well as the Source view (two tabs at the bottom of the editor). We have learned how to change the default port of Tomcat in the last chapter. You can easily change that in the Eclipse editor by opening server.xml and going to the Connector node. If you need to search the text, you can switch to the Source tab (at the bottom of the editor).

Figure 2.5: Open server.xml

You can also easily edit tomcat-users.xml to add/edit Tomcat users. Recall that we added a Tomcat user in Chapter 1, Introducing JEE and Eclipse, to administer the Tomcat server.

By default, Eclipse does not change anything in the Tomcat installation folder when you add the server in Eclipse. Instead, it creates a folder in the workspace and copies Tomcat configuration files to this folder. Applications that are deployed in Tomcat are also copied and published from this folder. This works well in development, when you do not want to modify Tomcat settings or any application deployed in the server. However, if you want to use the actual Tomcat installation folder, then you need to modify server settings in Eclipse. Double-click the server in the Servers view to open it in the editor.

Figure 2.6: Tomcat settings

Note the options under Server Locations. Select the second option, Use Tomcat installation, if you want to use the actual Tomcat installation folders for configuration and for publishing applications from within Eclipse.