Java and PostgreSQL Web Application Part 3

Introduction

Creating a Java web application that interacts with PostgreSQL is a complex process that involves quite a few steps. Our multi-part tutorial series takes you through every stage of the process with step-by-step instructions. The previous article in this series discussed the configuration of the dispatcher servlet and the view resolver. In this article, we’ll pick up where we left off, showing you you how to create and configure an MVC controller class.

Prerequisites

Before proceeding with the tasks outlined in this article, be sure you understand the configuration and code presented in the second article of the series.

Creating MVC Controller

In the previous installment of our tutorial series, we configured the dispatcher servlet and the view resolver of our Java and PostgreSQL web application. We’ll begin our work in this article by configuring a Spring MVC class controller.

First, we’ll create a package for the controller. The process is similar to the one described in the previous article. Right-click on “src”, then choose “New” and select “package”. In the “New Java Package” area, provide a package name; for this example, the package name is “com.chinito.carsmanager.controller”.

image shows a window for creating a new java package

Next, we’ll create a new class named “CarsController”. To do this, right-click on the package, then select “New” from the pop-up menu, then select “class”. This will open the “New Java Package” window.

Once we have created our class, we’ll make a simple handler return within the class to test our Java and PostgreSQL web application.

We’ll be using the following code:

package com.chinito.carsmanager.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CarsController {

@RequestMapping("/")
public ModelAndView home() {
ModelAndView modelView = new ModelAndView("index");

modelView.addObject("message","Hello World from MVC Spring");
return modelView;
}
}

There’s quite a bit going on in this block of code. Let’s take a closer look at it:

  • First, we declare a @Controller annotation. This lets us know that this class has the role of controller of our Java and PostgreSQL web application.

  • We also add a @RequestMapping() annotation, which maps the defined MVC handler methods.

  • Next, we instantiate the Spring interface ModelAndView. As the name implies, this interface holds both the model and view. Then we pass “index” as an argument; in this example, it will be the view file, which is our index.jsp file.

  • We then put a single object into the modelView. We can expect it to be displayed within the view file when we test the web application.

  • Finally, we create the index.jsp file inside the views folder that we created earlier. This file will be called once a request is made against the web application.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>

<title>Car Information System</title>
</head>

<h2>${message}</h2>
</body>
</html>

It’s time to test what we’ve done so far. We’ll need to create a new server within our Eclipse IDE to perform our testing.

We’ll follow the steps listed below:

image shows creating a new server in eclipse

First, click on No servers are available. Click this link to create a new server....

Then, click the Next button to open up the “Define a New Server” window. Select your server runtime environment— in this case, we will be using the Apache Tomcat v9.0. Then click Next.

image shows define a new server

This will bring up the “New Server” window. From here, simply add the resources corresponding to your project, then click the Finish button.

image shows the new server window

Now that the new server has been successfully created, you can right-click on it and select “Start” to run the server.

We can find out if any errors occurred on the server by going to the Console tab and inspecting the details. In this case, the server is running smoothly, so we’d see something like this:

image shows server is listening on port 8080

Once we verify that the server is running, we’ll navigate to our project via the browser. For this example, we’ll use the following URL: localhost:8080/CarInformationSystem/

We should see results that look like the following:

image shows the browser displaying the message from our eclipse application

The results in the browser prove that our Spring MVC is working, because it tells us that we’re able to pull out the string from the message object in our view controller and display it in the browser.

Conclusion

The multi-part tutorial series covers every step involved in the creation of a Java and PostgreSQL web application. In this article, we continued where we left off in our previous installment, explaining how to create the MVC controller class and configure it for our needs. With this detailed example to guide you, you’ll have no problem building your own web app using PostgreSQL and Java.

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.