How to create a web application from scratch with Java, Eclipse, Tomcat, Hibernate and PostgreSQL – Part #1

This is the first of a series of articles about the development of a simple web application based on Java technology. We will start from scratch, creating the project in the Eclipse development environment and creating servlets that will be deployed within the Tomcat web container and will receive requests, allowing us to implement our business logic and interact with a PostgreSQL database, passing through a persistence layer implemented by the Hibernate framework.

CREATE THE ECLIPSE PROJECT
From the Eclipse menu select “File” -> “New” -> “Dynamic Web Project” if it’s already available in the menu item list. Otherwise, under “File” -> “New”, choose “Other” and select from the wizard that appears, shown in the following picture, the “Web” item and then the type “Dynamic Web Project”.

Eclipse create new project

Choose the project name and complete its creation through the wizard. At this point the tree of the new project appears on the left side panel of Eclipse, called the Project Explorer. We will analyze in the following articles all the different parts of the application that are created by Eclipse, but for now we focus on these three main items:

  • The “Java Resources” folder -> src: this is the folder where we will create the source code of our classes.
  • The “WebContent” folder: this is the folder where we will create web pages of our application, both static (HTML) and dynamically generated by the server (JSP).
  • The “Deployment Descriptor”: that’s an XML file with a defined name, “web.xml”, which contains information about the application configuration.

The deployment descriptor is the core component of web applications and we will see later in detail what are the elements that are placed inside it and what information it contains.
We also create under the “WebContent” folder, which as mentioned above will contain our web pages, the main page of our application that we call, of course, “index.html”. We insert in this page a simple welcome message and a link that later will allow the user to access a new page where he can register. At that point a new user will be inserted into our system and stored into the database.
The following picture shows the code of our simple homepage and its location within the structure of the project.

Web application index page

However, before we can view the page in our browser, we have to configure an instance of Tomcat web container in which our application will be deployed and run.

CONFIGURE TOMCAT
To test our web application we need a web container and, as said earlier, we are going to use Apache Tomcat. In order to have it running, go to this page, download the version 7.0 of Tomcat and unpack it in a folder of the file system. Let’s go back in Eclipse and click with the right button on our project in the Project Explorer, then select the “Run As” item and finally “Run on Server” (the same can be done with keyboard shortcuts pressing Alt + Shift + X and then the R key).
This will open a wizard in which we indicate, through the appropriate checkboxes, that we want to configure a new server manually. The window that appears is the one shown in the following picture:

Eclipse - Tomcat server setup

Now, we select “Apache” as server type and then we select the Tomcat version we downloaded earlier, ie 7.0. We declare that the instance of Tomcat will run locally, keeping the value “localhost” for the field host name, and we assign a name to the brand new server. In the “Server runtime environment” field we must provide the reference to the Tomcat installation; to do this click on “Add ..” and in the new window that appears just keep as Name “Apache Tomcat v7.0”. Now we indicate in the “Tomcat installation directory” field the path of the folder that we have previously downloaded and unzipped.

Eclipse - Tomcat server setup

At this point we will see in our Project Explorer a new project called “Servers” in which we find the new server we just set up, with the name we assigned it.

THE FIRST EXECUTION OF THE PROJECT
Now that we have created and configured the Tomcat instance, we can try to run our application. To do this, we click with the right button on our project, select the menu item “Run As” and then the submenu item “Run on Server”. Tomcat is then launched and it is ready to run our application. As a result we will see a new tab opening in the central window of Eclispe, that will display contents of the index.html homepage we created.
The following image shows the result we get running this first simple application:

Web application first run

So far we have focused on creating the project and appropriately configuring the development environment, in order to get our application up and running. At the moment it’s merely a static web page that contains a welcome message and a link to another page for the creation of a new user. This new page is still not available and it can’t obviously be reached, but we will create it in the next article.
Properly display the welcome page shown in the previous picture represents our starting point and once this is achieved we can continue focusing on adding functionalities to our application.

The tutorial continues here: How to create a web application from scratch with Java, Eclipse, Tomcat, Hibernate and PostgreSQL – Part #2

See also:

This entry was posted in $1$s. Bookmark the permalink.

One thought on “How to create a web application from scratch with Java, Eclipse, Tomcat, Hibernate and PostgreSQL – Part #1

  1. Pingback: How to create a web application from scratch with Java, Eclipse, Tomcat, Hibernate and PostgreSQL – Part #2 | Dede Blog

Leave a Reply

Your email address will not be published. Required fields are marked *