In this example, the Eclipse Neon Java IDE (Integrated Development Environment) and the Apache Tomcat Web Server are used to setup a Java IDE for developing and running web applications that can contain Java Servlets, JavaServer Pages and REST- & SOAP web services. It is shown in this post how to create a Servlet and how to deploy and run a Java web application on Tomcat.
- For an example on how to setup a Spring Web MVC project from a Maven archetype in Eclipse Neon, please see this post:
- Eclipse Neon – Create Java Web Project with Spring – Without Maven
- Create Java Project in Eclipse Neon
- Export a Java Project in Eclipse Neon to Executable JAR File with Manifest
- Eclipse Neon – Export Dynamic Web Project to WAR File
- Setting up Maven on Mac OS and Creating Java Project
- Creating a Java SOAP Service with Eclipse and Tomcat
- Create JAX-RS Java REST Service with Eclipse and Tomcat
- Installation of Java Runtime Environment
- Installation of Apache Tomcat Web Server
- Installation of Eclipse IDE for Java EE Developers
- Setup of Tomcat Runtime Environment in Eclipse
- Setup of new Dynamic Web Project in Eclipse
- Creation of "Hello World" Servlet and JSP View
- Run the Dynamic Web Project in Eclipse
- Export as WAR File and Deployment to Tomcat
The open-source software used in this example can be downloaded from these sources:
1 Installation of Java Runtime Environment (JRE)
- Download the latest Java Runtime Environment for your operating system. I used JRE version 8, update 111 (released: October 18, 2016) for this post.
- Install the JRE by following the installation instructions.
- Confirm JRE installation by opening a command prompt and executing
- Confirm the
JAVA_HOMEenvironment variable setting by executing
- The Java Runtime Environment is now installed.
2 Installation of Apache Tomcat Web Server
- Download the Apache Tomcat web server. In this example, the binary distribution of version 9 for Windows 64-bit was used.
- Install the Tomcat web server by un-zipping the downloaded ZIP file to any location. Here,
c:\tomcat9was used as the root folder.
- Validate the Tomcat installation by navigating to the root folder and starting the web server by executing
- On Windows, a new CLI window will open, showing the Tomcat console output. A message saying
Server startup in XXXX msindicates that the web server started successfully.
- Access the Tomcat welcome page using
http://localhost:8080in a web browser to confirm that the Tomcat Server is running properly.
- Shutdown Tomcat by executing the file
bin\shutdown.batlocated in the root folder.
3 Installation of Eclipse IDE for Java EE Developers
- 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.
- Install the Eclipse Java EE IDE by unzipping the downloaded ZIP file into any location. Here,
c:\eclipse-neon-idewas used as the root folder.
- Start the Eclipse IDE by navigating to the root folder and executing
- 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.
- Click on OK to launch Eclipse and view the default Welcome page.
4 Setup of Tomcat Runtime Environment in Eclipse
In order to setup a Java IDE for web development, a runtime environment is needed. This is usually a Servlet container such as Apache Tomcat or an application server such as JBoss. In this example, Apache Tomcat is configured in Eclipse as the runtime environment for web projects.
- At first 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 to run and debug dynamic web projects on Tomcat from within Eclipse.
- To setup a new runtime environments, navigate to Window > Preferences.
- In the Preferences dialog, select Server > Runtime Environments and then click on the Add… button to setup a new runtime environment.
- In the New Server Runtime Environment dialog, select Apache Tomcat v9.0 from the list and then click on the Next button.
- Specify the installation directory of Tomcat by clicking on Browse and selecting the root folder of the Tomcat installation. In this example
c:\tomcat9was used as the root folder.
- Click on Finish to continue.
- 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.
5 Setup of new Dynamic Web Project in Eclipse
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.
- 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.
- 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.
- 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.
- In the New User Library dialog, enter a library name, here it is
- The new library now shows up in the User Library list. Click on Add External JARs to add the Tomcat JAR files to it.
- In the JAR Selection dialog, navigate to the Tomcat 9 “lib” folder. In this example, the complete folder path is
- Select all of the JAR files in that folder and click on “Open“.
- The JAR files are now listed under the newly created library.
- Click on OK to close the Preferences window.
- 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.
- In the tree menu, select Java Build Path and open the Libraries tab in the main area.
- Click on the button Add Library and select User Library in the Add Library dialog.
- Select the newly created user library, in this case, Tomcat9Runtime and click on Finish.
- The library is now added to the web project’s list of dependent libraries.
- When expanding the Java Resources > Libraries node in the Project Explorer tree menu, the JAR files from that library are now listed.
6 Creation of "Hello World" Servlet and JSP View
- In the Project Explorer, right click on the Servlets node and use New> Servlet in the context menu.
- 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.
- Click on Next to continue. The additional dialog shows the URL mapping for the new Servlet.
- 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
doPostwill 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
doGetmethod 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
- This is accomplished by using the
- 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.
- 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.
- 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 , where the JSP Expression Language is used to access a HTTP GET request parameter called name and prints its value.
- 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
- 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.
- 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.
- 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.
- 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 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:
- Any other external web browser can be used to access the running web project as well.
8 Export as WAR File and Deployment to Tomcat
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.
- In the WAR Export dialog window, enter the destination folder for the WAR file and click on the Finish button.
- 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.
- 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.xmlto add a user with the
manager-guirole as shown below.
- 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
- Access the Manager Application using a web browser and the default URL:
- 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.
- The deployed web application will be started automatically and is then listed in the Applications section of the Manager Application.
- Finally, the deployed web application can be accessed with a web browser by using the application’s context path and the Servlet URL.