Java and PostgreSQL Web Application PART 2

Introduction

If you’re a Java developer, you may find yourself wanting to build an application that interacts with a PostgreSQL database. This multi-part tutorial will explore this topic in detail, showing how to build a Java web application using the Eclipse IDE and several other technologies. In the first article of this series, we explained how our web application was going to work, and we created a database that we’ll be using throughout our tutorial. This second article in the series will proceed with the development of our Java and PostgreSQL web application, configuring some of the essential dependencies for the project.

Prerequisites

Be sure that you’ve read and understood the first article before proceeding with this one. The first installment in this series provides a helpful overview of the architecture of our Java and PostgreSQL web application. It also lists some technical prerequisites that need to be in place for this project.

Configuring the Java Dependencies

In the previous article, we converted our Java web dynamic project into a Maven project. This resulted in Java generating an XML file named “pom.xml”. This file, named for the Project Object Model, holds information about the configuration of the Java project that Maven used when converting the project.

That might sound a bit confusing, but it’s much easier to understand when you see an example of the file. Let’s take a look at the essential dependencies included in our “pom.xml” file:

<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelversion>4.0.0</modelversion>
<groupid>com.teamsolo</groupid>
<artifactid>CarsManager</artifactid>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>


<properties>
<spring.version>5.1.9.RELEASE</spring.version>
<hibernate.version>5.4.3.Final</hibernate.version>
</properties>

<dependencies>


<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-webmvc</artifactid>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-orm</artifactid>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupid>org.springframework.data</groupid>
<artifactid>spring-data-jpa</artifactid>
<version>2.1.5.RELEASE</version>
</dependency>



<dependency>
<groupid>org.hibernate</groupid>
<artifactid>hibernate-core</artifactid>
<version>${hibernate.version}</version>
</dependency>


<dependency>
<groupid>org.postgresql</groupid>
<artifactid>postgresql</artifactid>
<version>42.2.2</version>
<scope>runtime</scope>
</dependency>


<dependency>
<groupid>javax.servlet</groupid>
<artifactid>javax.servlet-api</artifactid>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupid>javax.servlet.jsp</groupid>
<artifactid>javax.servlet.jsp-api</artifactid>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupid>jstl</groupid>
<artifactid>jstl</artifactid>
<version>1.2</version>
</dependency>
</dependencies>

<build>
<sourcedirectory>src</sourcedirectory>
<plugins>
<plugin>
<artifactid>maven-compiler-plugin</artifactid>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactid>maven-war-plugin</artifactid>
<version>3.2.1</version>
<configuration>
<warsourcedirectory>WebContent</warsourcedirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>

We’re using the “properties” tag in our XML to hold the versions of Spring framework and Hibernate. We can then reference those versions as we define other dependencies. To reference this version information, we use ${spring.version} or ${hibernate.version}.

Configuring the Spring Dispatcher Servlet

Next, we’ll be configuring the Java Spring MVC for our web application by creating configuration Java class files. The two files we’ll be creating are:

  • WebInit.java – This file handles the requests made against our Java web application.

  • WebMVCconfig.java – This file configures the view resolver.

We’ll follow these steps in sequence:

  • First, create a new package in ECLIPSE. Right-click on “src”, then select “New”, then select “Package” to open the New Java Package window. In this window, provide a standard name for your package. Finally, click the “Finish” button to create the package.

Image shows New Java Package window

  • Create a new class under this package by right-clicking the package and selecting the new class; this will open the “Java Class Window”. In the window, provide a name for your Java class. For this example, we’ll be naming our class “WebInit”.

image shows the new java class window

  • The following code will be used in the WebInit.java class:
package com.chinito.carsmanager.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebInit implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext aplContext = new AnnotationConfigWebApplicationContext();
aplContext.register(webMVCconfig.class);

ServletRegistration.Dynamic despachador = servletContext.addServlet(
"SpringDispachador", new DispatcherServlet(aplContext));
despachador.setLoadOnStartup(1);
despachador.addMapping("/");
}

}

It might seem like there’s a lot going on in this code, so let’s break it down one section at a time:

  • First, we implement the onStatrtup method from the “WebApplicationInitializer”.
  • Then, we register a configuration class against the spring web MVC aplContext. The class name will be webMVCconfig.class. Don’t be concerned that this class doesn’t yet exist– we’ll be creating the file later.
  • We then register a dispatcher servlet named “SpringDispachador”.
  • We’ll set our dispatcher “SpringDispachador” to load on startup using the function despachador.setLoadOnStartup(1) and passing the value of “1”.
  • Finally, we add mapping using the method despachador.addMapping("/").

Configure the Spring View Resolver

In this section, we’ll configure the Spring MVC view resolver.

Shown below is the code we’ll need for this Java class:

package com.chinito.carsmanager.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan("com.chinito")
public class webMVCconfig {
// configure the view resolver for spring mvc

@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");

return viewResolver;
}
}

Let’s take a closer look at what’s going on in this code:

  • First, we declare a @Configuration annotation for this Java class. This will allow us to declare a @Bean method within this class, and that bean is named viewResolver.

  • Then, we use the Spring’s @ComponentScan() annotation, providing the package of this Java class which is “com.chinito”. The annotation allows Spring to scan this class.

  • Finally, we use Spring’s InternalResourceViewResolver as our view resolver to render the models in different browsers.

  • Spring allows us to use JSPs, the Velocity template and more.

  • Note that we specify a views folder in the viewResolver.sePrefix() method. This means that we need to create this folder. We do this by navigating to the WebContent folder, then right-clicking on “WEB-INF”. Then, we select “New”, followed by “Folder”. In the “New Folder” window, we type the word views as the name of the folder and click Finish to create the folder.

image shows creating a folder inside WEB-INF.

Conclusion

Building a Java and PostgreSQL web application is a complex project with quite a few steps involved in the process. In this second part of our series, we showed how to configure the essential dependencies of our web application using the pom.xml file, and we configured both the dispatcher servlet and the view resolver. We’ll move forward in the next installment of the series, showing you the next steps in the development of our web application.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.