Build a Java EE Application with Hibernate Framework : Part 3— Creating Our First Servlet

Yassine El Baaj
3 min readNov 23, 2019

--

In the previous tutorial, we configured Hibernate and created our models. In this tutorial, we will create our first HttpServlet class : “CatalogServlet”. It will allow us to fetch all the records within our database and display them in our Home Page.

Creating The Database Connection Class

It would be really repetitive and tedious to have to establish a connection between the web application and the database every time we want to communicate with it. To avoid this, we will create a Class that encompasses all the information related to the database ( Login name, Password, Driver etc…). This Class will only be instantiated once (Singleton Design Pattern ) , hence, it will exist as long as the application is running.

Create a new Class within your project. Name it “HibernateConnection”.

Singleton Connection Class

As you can see, we are building ( or configuring ) a SessionFactory object using the properties defined in the file “hibernate.cfg.xml” with the method : new Configuration().configure() . We are also adding the persistent classes that we have created previously.

Creating The Servlet

  • Right-Click on your project and go to New>Servlet.
  • Enter “CatalogServlet” and click on “Next”.
  • In the “URL mappings” section, select “/CatalogServlet” et choose “Edit”. Set the new URL path to “/catalog”.
  • Add a new URL path : “/”.
  • Click on “Finish”.

Your newly created Servlet Class has two methods : doGet() and doPost(). Following are the endpoints that we will implement in our Servlet :

  • /”: Mapped to a page that displays the number of records regarding each model.
  • /catalog?entity=books” : Mapped to a page that displays all the existing books.
  • /catalog?entity=authors” : Mapped to a page that displays all the existing authors.
  • /catalog?entity=genres” : Mapped to a page that displays all the existing genres.

Note that when clicking on a particular book, author or genre, we should be redirected to another page that displays it details.

doGet() Method

Before performing any query to the database, we need to establish the connection. The following code should be inserted in the body of your class :

Establishing the connection with the database

We instantiated a SessionFactory object, which will then be used to created a Session object. This latter will allow us to initiate a physical connection with our database.

In order to perform database operations we need to instantiate a Transaction object from the previously created Session object.

  • The createCriteria() method allows us to represent a query against a particular persistent class.
  • the setProjection() method allows us to apply a Projection to query. In this case, our Projection is Projections.rowCount() : Aims to get the number of rows in our table.
  • We render the row counts to the JSP page.

We will now write the code to render all the existing records of a particular model in our JSP. Since we are specifying which records we want to display via the URL, we will use the method : request.getParameter().

We are getting all the records for each model via the loadAllData() method. The result is stored in a List, which we render via the method : request.setAttribute(“entities”,entities).

Following is the code for the loadAllData() method :

  • We first create a CriteriaBuilder object that will allow us to construct a query.
  • The createQuery() method allows us to specify against which Class we will be performing our query.
  • The getResultList() method executes a SELECT query and returns the query results as an untyped List.

Following is the code of our entire CatalogServlet Class.

CatalogServlet

Creating Our JSP File

  • Right-Click on your project, go to New>JSP File and enter “index.jsp
  • Add the following tag in the top of your file :
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core" prefix=”c” %>
  • To display the number of records without writing any Java code within our JSP, we use the JSTL library that we just imported :
  • Create another JSP file and name it “catalog.jsp”.
  • To display a List using the JSTL library, we use the <c:forEach> Tag.

Since our database is still empty, you will not be displayed anything.

What is next

In the next tutorial, we will create a new Servlet that will allow us to insert data in our database.

--

--