Java and PostgreSQL Web Application PART 2


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.


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="">













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:

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

  • – 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 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.servlet.DispatcherServlet;

public class WebInit implements WebApplicationInitializer {

public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext aplContext = new AnnotationConfigWebApplicationContext();

ServletRegistration.Dynamic despachador = servletContext.addServlet(
"SpringDispachador", new DispatcherServlet(aplContext));


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;

public class webMVCconfig {
// configure the view resolver for spring mvc

@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

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.


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.