Create and Run Java Servlet Web App (2.5) Using Eclipse and Apache Tomcat

Fernando Christyanto
3 min readJul 13, 2019

--

What needed to be installed

Installing the Web Plugins for Eclipse

Open eclipse, and setup your preferred workspace location.

In the menu bar, click help and choose Install New Software.

Choose 2019–06 — http://download.eclipse.org/releases/2019-06 in the dropdown, check the Web and Database plugins, then click finish and install the plugins. Wait for the installation to finish, then eclipse might prompt for a restart, go ahead and restart eclipse.

Eclipse Plugins Installation

Creating a new Servlet Project

Run eclipse, choose File > New > Other > Web > Dynamic Web Project.

Input the project name, and set the Dynamic Web Module version to 2.5.. I’ll leave other settings as their defaults.

New Project Configuration

After the project has been generated, right click on the project name in the project explorer tab, and choose Build Path > Configure Build Path.

Open Library tab, and click Add External JARs. Choose the servlet-api.jar (You can get the jar file here https://drive.google.com/open?id=1iHivq8ySHXDoQeTSzg7-My7zFjyk-T_H) *Original jar link: http://www.java2s.com/Code/JarDownload/servlet/servlet-api.jar.zip. Then “Apply and Close”.

Let’s create our first page shall we…

Right click on WebContent folder and create a new .jsp file. I’ll name the file home.jsp.

Open home.jsp, and replace the content with this

Creating the servlet that acts as the handler

Open Java Resources folder, there you’ll see a package named src. Right click on src, create a new package, name the package controller.

Right click on controller package, create a new Servlet file (I’ll name the file HomeController)

Open HomeController.java, replace the content with the code below..

One Last Check…

Open WebContent > WEB-INF > web.xml. By now, your web.xml should probably look like this. (Make sure the servlet you created before appear inside the servlets entry, and it is mapped to a url).

Running the application with Apache Tomcat

Because we previously set the Dynamic Web Module version to 2.5, we will use Apache Tomcat version 6.0.xx

Firstly, Download apache tomcat version 6.0 (https://archive.apache.org/dist/tomcat/tomcat-6/v6.0.53/bin/), download the apache-tomcat-6.0.53.zip and extract the zip.

Back to eclipse, Right click on the project name > Run As > Run on Server. For the server type, choose Apache Tomcat v6.0 Server.

Click next, then you’ll be prompted to provide the Tomcat Installation Directory. Choose the previously extracted apache-tomcat-6.0.53 folder.

Click finish, and the Tomcat server should start initializing.

After the server has run, open your web browser, and try opening http://localhost:8080/ServletHelloWorld/HomeController, and you should be able to see this

Hope this helps!

This sample Hello World project is available here: https://github.com/fernandochristyanto/ServletHelloWorld

--

--