Setup of Java EE Development Environment with Eclipse Neon and Tomcat 9

In this example, the Eclipse Java EE IDE (Java Enterprise Edition – Integrated Development Environment) and the Apache Tomcat Web Server are used to setup and configure a Java EE development environment for developing and testing Java web applications. This setup allows Java developers to create web applications that contain Java Servlets, JavaServer Pages and REST- & SOAP web services.

Related Posts

Summary

  1. Installation of Java Runtime Environment
  2. Installation of Apache Tomcat Web Server
  3. Installation of Eclipse IDE for Java EE Developers
  4. Setup of Tomcat Runtime Environment in Eclipse
  5. Setup of new Dynamic Web Project in Eclipse
  6. Creation of "Hello World" Servlet and JSP View
  7. Run the Dynamic Web Project in Eclipse
  8. Export as WAR File and Deployment to Tomcat

Software Downloads

The open-source software used in this example can be downloaded from these sources:

Java Runtime Environment from Oracle (e.g version 8, update 111)
Apache Tomcat Web Server (here version 9)
Eclipse Java EE IDE from the Eclipse Downloads page (e.g Neon 1A)

  • Download the latest Java Runtime Environment for your operating system. I used JRE version 8, update 111 (released: October 18, 2016) for this post.

Oracle Java JRE Version 8 Download Page

  • Install the JRE by following the installation instructions.

Oracle Java JRE Version 8 Installation on Windows

  • Confirm JRE installation by opening a command prompt and executing java -version
  • Confirm the JAVA_HOME environment variable setting by executing  set JAVA_HOME

Oracle Java JRE 8 Check Java Version and Java Home on Windows 7 Command Line

  • The Java Runtime Environment is now installed.

  • Download the Apache Tomcat web server. In this example, the binary distribution of version 9 for Windows 64-bit was used.

Apache Tomcat 9 Download Page

Note: If Tomcat is installed as a Windows service, some steps in this section will be different.
  • Install the Tomcat web server by un-zipping the downloaded ZIP file to any location. Here, c:\tomcat9 was used as the root folder.

Apache Tomcat 9 ZIP File for Windows x64

  • Validate the Tomcat installation by navigating to the root folder and starting the web server by executing bin\startup.bat

Apache Tomcat 9 bin folder and startup.bat file

  • On Windows, a new CLI window will open, showing the Tomcat console output. A message saying Server startup in XXXX ms indicates that the web server started successfully.

Apache Tomcat 9 Startup Console Output on Windows 7 Command Line

  • Access the Tomcat welcome page using http://localhost:8080 in a web browser to confirm that the Tomcat Server is running properly.

Apache Tomcat 9.0.0 M11 Welcome Page

  • Shutdown Tomcat by executing the file bin\shutdown.bat located in the root folder.

Apache Tomcat 9 bin folder shutdown.bat Windows 7

  • Download the latest Eclipse Java EE IDE for your operating system. For example, for Eclipse Neon 1A on 64-bit Windows 7, go to the Eclipse Neon website and use the Windows 64-bit link.

Eclipse IDE for Java EE Developers - Download Page - Neon Package

  • Install the Eclipse Java EE IDE by unzipping the downloaded ZIP file into any location. Here, c:\eclipse-neon-ide was used as the root folder.

Eclipse IDE for Java EE Developers - ZIP File for Windows 64-bit

  • Start the Eclipse IDE by navigating to the root folder and executing eclipse.exe

Eclipse IDE for Java EE Developers - Startup with Eclipse.exe on Windows 7 64-bit

  • When starting Eclipse for the first time, it may be necessary to set the default workspace. Select any location and check the checkbox Use this as the default and do not ask again.

Eclipse IDE for Java EE Developers - Initial Start - Select Workspace

  • Click on OK to launch Eclipse and view the default Welcome page.

  • At the initial launch, Eclipse will show the Welcome page. Before clicking on any of the links, it is recommended to setup the Apache Tomcat 9 web server runtime environment in Eclipse.
  • This allows a developer to run and debug dynamic web projects on Tomcat from within Eclipse.
  • To setup a new runtime environments, navigate to Window > Preferences.

Eclipse IDE for Java EE Developers - Menu > Window > Preferences

  • In the Preferences dialog, select Server > Runtime Environments and then click on the Add… button to setup a new runtime environment.

Eclipse IDE for Java EE Developers - Preferences > Server Runtime Environments > Add

  • In the New Server Runtime Environment dialog, select Apache Tomcat v9.0 from the list and then click on the Next button.

Eclipse IDE for Java EE Developers - Preferences > Server Runtime Environments > New Server Runtime Environment

  • Specify the installation directory of Tomcat by clicking on Browse and selecting the root folder of the Tomcat installation. In this example c:\tomcat9 was used as the root folder.
  • Click on Finish to continue.

Eclipse IDE for Java EE Developers - Preferences > Server Runtime Environments > New Tomcat Server Runtime Environment

  • The Apache Tomcat v9.0 web server is now available as a server runtime environment in Eclipse and can now be used to deploy and run Java web modules containing Servlets, JSP files, web services and so on. Click on OK to close the Preferences dialog.

Eclipse IDE for Java EE Developers - Preferences > Server Runtime Environments > Apache Tomcat v9.0

A Dynamic Web Project in Eclipse is a project facet for developing Java web applications as defined in the Servlet Specification. It generates a web application deployment descriptor (web.xml) and allows to export the project to a web archive (WAR) file for deployment to a Servlet container such as Tomcat.

  • On the Eclipse Welcome page, select Create a new Java EE Web Project or use the Eclipse main menu and select File > New> Dynamic Web Project. Both links will show the same dialog for configuring a new dynamic web project.

Eclipse IDE for Java EE Developers > Create a new Java EE Web Project

  • In the Dynamic Web Project dialog, enter the project’s name and select the target runtime, in this case Apache Tomcat v9.0.
  • Select the Dynamic web module version, in this case it is 3.1, indicating Servlet Specification 3.1, then click on the Finish button.

Eclipse IDE for Java EE Developers > Create a Dynamic Web Project

  • The new dynamic web project will now show up in the Project Explorer of Eclipse. To open the Project Explorer, navigate to Window> Show View > Project Explorer in the Eclipse main menu.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project

  • It may be necessary to configure the project’s build path so that the Tomcat 9 JAR files are added as a dependent library to the project. These JAR file include the Servlet API, Annotations API and others.
  • Create a new user library by navigating to Window > Preferences in the Eclipse main menu.
  • In the tree menu of the Preferences dialog, navigate to Java> Build Path> User Libraries.
  • Click on the New button to add a new user library.

Eclipse IDE for Java EE Developers > Preferences > Java > Build Path > User Libraries

  • In the New User Library dialog, enter a library name, here it is Tomcat9Library

Eclipse IDE for Java EE Developers > Preferences > Java > Build Path > User Libraries > New User Library

  • The new library now shows up in the User Library list. Click on Add External JARs to add the Tomcat JAR files to it.

Eclipse IDE for Java EE Developers > Preferences > Java > Build Path > User Libraries > Add External JARs

  • In the JAR Selection dialog, navigate to the Tomcat 9 “lib” folder. In this example, the complete folder path is c:\tomcat9\lib.
  • Select all of the JAR files in that folder and click on “Open“.

Apache Tomcat 9 lib folder JAR files

  • The JAR files are now listed under the newly created library.
  • Click on OK to close the Preferences window.

Eclipse IDE for Java EE Developers > Preferences > Java > Build Path > User Libraries > Tomcat 9 JAR files

  • Adding the library to the web project: In the Eclipse Project Explorer, right click on the project name and select Properties from the context menu.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > Properties

  • In the tree menu, select Java Build Path and open the Libraries tab in the main area.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > Properties > Java Build Path > Libraries

  • Click on the button Add Library and select User Library in the Add Library dialog.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > Properties > Java Build Path > Libraries > Add Library > User Library

  • Select the newly created user library, in this case, Tomcat9Runtime and click on Finish.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > Properties > Java Build Path > Libraries > Add Library > Select User Library to add to the classpath

  • The library is now added to the web project’s list of dependent libraries.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > Properties > Java Build Path > User Library

  • When expanding the Java Resources > Libraries node in the Project Explorer tree menu, the JAR files from that library are now listed.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > Java Resources > Libraries > User Library

  •  In the Project Explorer, right click on the Servlets node and use New> Servlet in the context menu.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > New Servlet

  • Alternatively, you can use the Eclipse main menu and navigate to File > New > Servlet.
  • In the Create Servlet dialog, enter the Java package and class name of the Servlet.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > Create Servlet

  • Click on Next to continue. The additional dialog shows the URL mapping for the new Servlet.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > Create Servlet > URL Mappings

  • Click on Finish to complete the process. Eclipse will generate the Servlet and show the generated Java code in the Java editor. By default, the HTTP request handlers doGet and doPost will be generated. In the Model-View-Controller design pattern, this generated Servlet implements the Controller, i.e. it should be used to perform business logic, access data, the Model, and then return a View. In Java web applications, views are implemented using Java Server Pages (JSP) files.
  • In this example, the doGet method of the Servlet has been changed to include one line of code that forwards the request and response objects to a JSP view file called HelloWorldView.jsp.
  • This is accomplished by using the HTTPServletRequest.getRequestDispatcher method.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > Java Editor - HelloWorldServlet code

request.getRequestDispatcher("/HelloWorldView.jsp").forward(request, response);
  • To create the HelloWorldView.jsp file, right click on the project name in the Project Explorer and select New > JSP File.
  • In the New JSP File dialog, keep the WebContent as the destination folder and enter the JSP file name, then click on “Next” to continue.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > New JSP File

  • In the Select JSP Template dialog, a developer can select from a default list of templates or select a custom JSP template. Here, the default template for a JSP file with HTML markup is selected.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > New JSP File > Select JSP Template

  • Click on Finish to complete the process. Eclipse will show the new JSP file in the HTML editor.
  • In this example, the JSP was modified to output a message saying Hello World! – Welcome ${param[“name”]}, where the JSP Expression Language is used to access a HTTP GET request parameter called name and prints its value.

Eclipse IDE for Java EE Developers > Project Explorer > Dynamic Web Project > Java Editor > HelloWorldView.jsp code

  • The Dynamic Web Project can be executed and debugged within Eclipse. For that, a server needs to be added to the project.
  • In the bottom area of the Eclipse IDE, switch to the Servers tab and right click to open a context menu. Then select New > Server

Eclipse IDE for Java EE Developers > Servers > New Server

  • In the Define a New Server dialog, select the previously configured Apache Tomcat v9.0 runtime environment from the dropdown control and click on the Next button.

Eclipse IDE for Java EE Developers > Servers > Define a New Server > Apache Tomcat v9.0

  • Use the Add > button to move the dynamic web project to the Configured list and then click on Finish to complete adding the Tomcat 9 server to the project.

Eclipse IDE for Java EE Developers > Servers > New Server > Add and Remove Projects

  • The Tomcat v9.0 server is now listed on the Servers tab and shows the web project that has been added to it. The server can be started by right clicking on the server name and selecting Start from the context menu.

Eclipse IDE for Java EE Developers > Servers > Start Server

  • The Tomcat 9 server will start and the dynamic web project will be automatically deployed and started as well. The Console tab in Eclipse will show the server status and any errors that occur when running the web project.

Eclipse IDE for Java EE Developers > Servers > Standard Output Console

  • Eclipse provides a built-in web browser that can be used to access the JSP file. Switch to the Internal Web Browser tab or use Window > Show View > Other > Internal Web Browser and enter the URL:
http://localhost:8080/SampleWebProject/HelloWorldServlet?name=John Smith
  • Any other external web browser can be used to access the running web project as well.

Eclipse IDE for Java EE Developers > Internal Web Browser

In order to deploy a dynamic web project on a Java EE compliant application server (such as JBoss or IBM Websphere) or on a Servlet container (such as Tomcat), the project must be packaged. This is achieved by creating a web archive (WAR) file. The WAR file contains all of the project’s runtime files and dependent JAR files, if any. In Eclipse, a WAR file can be generated by exporting a dynamic web project.

  • Right click on the project name in the Eclipse Project Explorer and select Export > WAR file.

Eclipse IDE for Java EE Developers > Dynamic Web Project > Export > WAR file

  • In the WAR Export dialog window, enter the destination folder for the WAR file and click on the Finish button.

Eclipse IDE for Java EE Developers > Dynamic Web Project > Export > WAR Export

  • The WAR file is essentially a ZIP file that contains all of the project’s file that are needed for running it. The source files are not included by default. The file can be unzipped (use 7-ZIP for example) to examine its contents.

Eclipse IDE for Java EE Developers > Dynamic Web Project > Export > WAR Export > View Contents of Generated WAR File with 7-ZIP

  • In Tomcat, the WAR file can be deployed by using the Manager Application that comes with Tomcat. JBoss and IBM Websphere offer similar administrative web applications.
  • By default, the manager application is disabled for security reasons. To enable it, edit $CATALINA_BASE\conf\tomcat-users.xml to add a user with the manager-gui role as shown below.

Apache Tomcat 9 > tomcat-users.xml > Add Manager Application User with role manager-gui

  • Refer to the official Tomcat 9 documentation on how to use the Manager Application.
  • Make sure to stop the Tomcat server in Eclipse and to start Tomcat as stand-alone as shown in section 2 of this post using  $CATALINA_BASE\bin\startup.bat
  • Access the Manager Application using a web browser and the default URL:
http://localhost:8080/manager
  • When promped enter the user name and password as set in the tomcat-users.xml file.
  • Scroll down to the WAR file to deploy section and use the Choose File button to select a WAR file from the local disk, then click on the Deploy button.

Apache Tomcat 9 > Manager Application > WAR file to deploy

  • The deployed web application will be started automatically and is then listed in the Applications section of the Manager Application.

Apache Tomcat 9 > Manager Application > Running Applications

  • Finally, the deployed web application can be accessed with a web browser by using the application’s context path and the Servlet URL.

Accessing HelloWorld Sample Web Application Deployed on Tomcat 9 from Chrome