Eclipse And Tomcat

broken image


When you start learning Spring MVC development configuringTomcat server in Eclipse Neon IDE can be a difficult task.This tutorial will help you setup your development environment.

2 Comments / Spring Tutorial / Eclipse, Maven, Tomcat After you create a maven project in eclipse for spring application development, you always need to use maven to clean, build, install, and deploy the spring application to a tomcat server to see the result. This article will tell you how to make maven deploy to tomcat in eclipse project. 1 You can set a timeout on startup and shutdown for your Tomcat server in Eclipse. If these timeouts are exceeded, Eclipse will pop up a message asking you if you want to kill it, or keep waiting. To set these, double-click the name of the server in your Servers tab. Feb 27, 2019 Start the Eclipse workbench. Open Window - Preferences - Server - Installed Runtimes to create a Tomcat installed runtime. Click on Add to open the New Server Runtime dialog, then select your runtime under Apache (Apache Tomcat v7.0 in this example). Click Next, and fill in your Tomcat installation directory. Modern IDEs like Eclipse provide various Plugins to ease web developement. However, I believe that starting Tomcat as 'normal' Java application still provides the best debugging experience.

Installing Tomcat 8.5

  • Visit Apache Tomcat download page and grab apache-tomcat-8.5.6.tar.gz file. You can also download file from terminal:
  • To make things easier you will install Tomcat inside your home directory. I assume that you already installed Java on your box, if not now is the time to do this. To instal Java follow tutorial at webupd8.
  • Let's check if everything is OK by starting Tomcat and opening http://localhost:8080 in your favorite browser:

You should see Tomcat welcome page:

Add

Eclipse And Tomcat Integration

  • Now we will add an administrator account on Tomcat server. This is not needed by Eclipse but it may come in handy when you will later want to play with Tomcat:

Now you must add the following line to tomcat-users.xml file:

Here we are creating admin user with 1234 password and we give him (or her)permissions to administration gui.Let's start Tomcat again (using ./catalina.sh run) and check thatwe can visit Tomcat manager page (located at http://localhost:8080/manager/html):

Configuring Eclipse Neon

Before we start check that you have following plugins installed:

Now we must create new Server Runtime Environment. Go to Preferences -> Server -> Runtime Environment -> Add…

Then specify path to your Tomcat installation (/home/user_name/tomcat)and optionally JRE (use Oracle Java 8 if available) and click Finish.

There is no Tomcat 8.5 in my wizard (if you completed previous steps skip this secion)

There is a bug in Eclipse that prevents some users from seeingTomcat 8.5 option in their wizards:

The solution is simple, we manually change Tomcat version number sayfrom 8.5.6 to 8.0.8.5.6 and this will allow us to use newer Tomcat versionwith buggy Eclipse.More details can be found in this SO question.

Following script will will patch Tomcat server:

After running script try to start Tomcat server to check if it is working.Then if everything is OK create new Runtime Environment in Eclipse using Tomcat 8 option.

Starting Tomcat from Eclipse

Open Servers window (press Ctrl+3 and write in search box Servers).Use link in Servers window to open New Server wizard and create a new server:

And

You should see new server created:

Click on server and choose Clean option, then click again and select Start.If you have Tomcat running in terminal (./catalina.sh run) stop it before you start Tomcat via Eclipse. Console window should display variouslog entries from starting Tomcat. Now click on server and select Stop.

Now it's time to restart Eclipse and check if everything works afterrestart.

Creating simple Spring MVC app

We will create simple Spring MVC application to check if everything is working.Let's start by creating new Maven project (New Project -> Maven Project and check Create simple project):Don't forget to select WAR as packaging method.

Open your POM file in XML editor and add following lines to it:

Then let's add four more files to our project:

Now click on pom.xml select Run As -> Maven install, this shouldcreate hellotomcat-0.0.1-SNAPSHOT.war file in target directory.

Click on project select Run As -> Run Configurations, then select Apache Tomcat and press New button.Click Apply and Close.

Click on project select Run As -> Run on Server:and Finish.

Go to http://localhost:8080/hellotomcat/ and check if you see greetings.Now open HomeController file change text and save changes, after a fewseconds refresh browser page - you should see your new text.

Join the DZone community and get the full member experience.

Join For Free
Eclipse And Tomcat

Modern IDEs like Eclipse provide various Plugins to ease web developement. However, I believe that starting Tomcat as 'normal' Java application still provides the best debugging experience. Most of the time, this is because these tools launch Tomcat or any other servlet container as external process and then attach a remote debugger on it. While you're still able to set breakpoints and inspect variables, other features like hot code replacement don't work that well. Also, plugins like Eclipse WTP don't really improve the startup time, stability and memory footprint of Eclipse ;-)

Therefore I prefer to start my Tomcat just like any other Java application from within Eclipse. Here's how it works:

This article addresses experienced Eclipse users. You should already know how to create projects, change their built path and how to run classes. If you need any help, feel free to leave a comment or contact me.

We'll add the Tomcat as additional Eclipse project, so that paths and all remain platform independent. (I even keep this project in our SVN so that everybody works with the same setup).

Step 1 - Create new Java project named 'Tomcat7'

Step 2 - Remove the 'src' source folder

Step 3 - Download Tomcat (Core Version) and unzip into our newly created project. This should now look something like this:

Step 4 - If you havn't, create a new Test project which contains your sources (servlets, jsp pages, jsf pages...). Make sure you add the required libraries to the built path of the project

Step 5.1 - Create a run configuration. Select our Test project as base and set org.apache.catalina.startup.Bootstrap as main class.

Step 5.2 - Optionally specify larger heap settings as VM arguments. Important: Select the 'Tomcat' project as working directory (Click on the 'Workspace' button below the entry field.

Add apache tomcat to eclipse

Step 5.3 - Add bootstrap.jar and tomcat-juli.jar from the Tomcat7/bin directory as bootstrap classpath.Add everything in Tomcat7/lib as user entries. Make sure the Test project and all other classpath entries (i.e. maven dependencies) are below those.

Tomcat

Eclipse And Tomcat Integration

  • Now we will add an administrator account on Tomcat server. This is not needed by Eclipse but it may come in handy when you will later want to play with Tomcat:

Now you must add the following line to tomcat-users.xml file:

Here we are creating admin user with 1234 password and we give him (or her)permissions to administration gui.Let's start Tomcat again (using ./catalina.sh run) and check thatwe can visit Tomcat manager page (located at http://localhost:8080/manager/html):

Configuring Eclipse Neon

Before we start check that you have following plugins installed:

Now we must create new Server Runtime Environment. Go to Preferences -> Server -> Runtime Environment -> Add…

Then specify path to your Tomcat installation (/home/user_name/tomcat)and optionally JRE (use Oracle Java 8 if available) and click Finish.

There is no Tomcat 8.5 in my wizard (if you completed previous steps skip this secion)

There is a bug in Eclipse that prevents some users from seeingTomcat 8.5 option in their wizards:

The solution is simple, we manually change Tomcat version number sayfrom 8.5.6 to 8.0.8.5.6 and this will allow us to use newer Tomcat versionwith buggy Eclipse.More details can be found in this SO question.

Following script will will patch Tomcat server:

After running script try to start Tomcat server to check if it is working.Then if everything is OK create new Runtime Environment in Eclipse using Tomcat 8 option.

Starting Tomcat from Eclipse

Open Servers window (press Ctrl+3 and write in search box Servers).Use link in Servers window to open New Server wizard and create a new server:

You should see new server created:

Click on server and choose Clean option, then click again and select Start.If you have Tomcat running in terminal (./catalina.sh run) stop it before you start Tomcat via Eclipse. Console window should display variouslog entries from starting Tomcat. Now click on server and select Stop.

Now it's time to restart Eclipse and check if everything works afterrestart.

Creating simple Spring MVC app

We will create simple Spring MVC application to check if everything is working.Let's start by creating new Maven project (New Project -> Maven Project and check Create simple project):Don't forget to select WAR as packaging method.

Open your POM file in XML editor and add following lines to it:

Then let's add four more files to our project:

Now click on pom.xml select Run As -> Maven install, this shouldcreate hellotomcat-0.0.1-SNAPSHOT.war file in target directory.

Click on project select Run As -> Run Configurations, then select Apache Tomcat and press New button.Click Apply and Close.

Click on project select Run As -> Run on Server:and Finish.

Go to http://localhost:8080/hellotomcat/ and check if you see greetings.Now open HomeController file change text and save changes, after a fewseconds refresh browser page - you should see your new text.

Join the DZone community and get the full member experience.

Join For Free

Modern IDEs like Eclipse provide various Plugins to ease web developement. However, I believe that starting Tomcat as 'normal' Java application still provides the best debugging experience. Most of the time, this is because these tools launch Tomcat or any other servlet container as external process and then attach a remote debugger on it. While you're still able to set breakpoints and inspect variables, other features like hot code replacement don't work that well. Also, plugins like Eclipse WTP don't really improve the startup time, stability and memory footprint of Eclipse ;-)

Therefore I prefer to start my Tomcat just like any other Java application from within Eclipse. Here's how it works:

This article addresses experienced Eclipse users. You should already know how to create projects, change their built path and how to run classes. If you need any help, feel free to leave a comment or contact me.

We'll add the Tomcat as additional Eclipse project, so that paths and all remain platform independent. (I even keep this project in our SVN so that everybody works with the same setup).

Step 1 - Create new Java project named 'Tomcat7'

Step 2 - Remove the 'src' source folder

Step 3 - Download Tomcat (Core Version) and unzip into our newly created project. This should now look something like this:

Step 4 - If you havn't, create a new Test project which contains your sources (servlets, jsp pages, jsf pages...). Make sure you add the required libraries to the built path of the project

Step 5.1 - Create a run configuration. Select our Test project as base and set org.apache.catalina.startup.Bootstrap as main class.

Step 5.2 - Optionally specify larger heap settings as VM arguments. Important: Select the 'Tomcat' project as working directory (Click on the 'Workspace' button below the entry field.

Step 5.3 - Add bootstrap.jar and tomcat-juli.jar from the Tomcat7/bin directory as bootstrap classpath.Add everything in Tomcat7/lib as user entries. Make sure the Test project and all other classpath entries (i.e. maven dependencies) are below those.

Now you can 'Apply' and start Tomcat by hitting 'Debug'. After a few seconds (check the console output) you can go to http://localhost:8080/examples/ and check out the examples provided by Tomcat.

Step 6 - Add Demo-Servlet - Go to our Test project, add a new package called 'demo' and a new servlet called 'TestServlet'. Be creative with some test output - like I was...

Step 7 - Change web.xml - Go to the web.xml of the examples context and add our servlet (as shown in the image). Below all servlets you also have to add a servlet-mapping (not shown in the image below). This looks like that:


test
/demo/test

Hit save and restart tomcat. You should now see your debug output by surfing to http://localhost:8080/examples/demo/test - You now can set breakpoints, change the output (thanks to hot code replacement) and do all the other fun stuff you do with other debugging sessions.

Hint:Keeping your JSP/JSF files as well as your web.xml and other resources already in another project? Just create a little ANT script which copies them into the webapps folder of the tomcat - and you get re-deployment with a single mouse click. Even better (this is what we do): You can modify/override the ResourceResolver of JSF. Therefore you can simply use the classloader to resolve your .xhtml files. This way, you can keep your Java sources and your JSF sources close to each other. I will cover that in another post - The fun stuff starts when running multi tenant systems with custom JSF files per tenant. The JSF implementation of Sun/Oracle has some nice gotchas built-in for that case ;-)

Eclipse And Tomcat

Opinions expressed by DZone contributors are their own.

Eclipse And Tomcat Tutorial

Popular on DZone





broken image