Spring MVC based login application using JDBC template

In this tutorial, we will create a simple login application using Spring MVC, in which we will be getting familiar to the basic Spring MVC application flow, the MVC based layered architecture with Spring and the interaction with database using Jdbc template. We will also understand the usage of @ModelAttribute and bean validation with the Validator API.

1. Maven Dependencies

Quickly moving ahead with the application, let’s first configure the dependencies in pom.xml file of our application.

pom.xml


2. Setup web.xml

Next task is to configure the first entry point of a web application, i.e web.xml.

web.xml

Location: /WebContent/WEB-INF/

3. Setup Dispatcher Servlet context xml

Create and configure the mvc-dispatcher-servlet.xml that will have all the configuration beans needed to handle the user requests.

This file contains spring-related configuration. The name dispatcher-servlet.xml comes from <servlet-name> configured in web.xml suffixed with “-servlet.xml”. Also, make sure to add namespace and schema location for MVC context.

mvc-dispatcher-servlet.xml

Location: /WebContent/WEB-INF/

4. Create Views

Create the view pages, index.jsp, login.jsp and welcome.jsp inside the webapp directory.

index.jsp

login.jsp

welcome.jsp

4. Create Java Entities

Now let’s start with creating the Java entity classes UserBean.java inside the package com.jCombat.bean.

UserBean.java

5. Creating DAO and Service layer classes

Create the DAO interface as ICombatDAO.java under the package com.jcombat.dao as –

Create the DAO class as CombatDAO.java in the package com.jcombat.dao as –

In the above snippet, note that setter injection of the dataSource property happens via autowiring by Type.

Note that there is only one bean of the type DataSource defined in the dispatcher-servlet.xml. Had there been more than one matching beans by Type, there would have been a Type conflict while autowiring. The spring container in that case would have switched to try autowiring by Name. Still if the autowired-property-variable-name do not make a match any that of the bean ids defined (via XML configuration) or Component/Qualifier names specified (via Annotations), autowiring would have failed then-and-there, with a nice exception thrown. We will be exploring more on the @Qualifier annotation soon some time later.

Similarly create service interface as ICombatService.java in package com.jCombat.service, as –

ICombatService.java

Create service class as CombatService.java in package com.jCombat.service, as –

CombatService.java

Likewise, in the above snippet too, field based injection of combatDAO happens via autowiring by Type. There is only one component we have defined of the type ICombatDAO. Hence, there are no Type conflicts (since there is one and only one component of the type ICombatDAO defined), hence the CombatDAO implementation-object-reference injects itself to the @Autowired combatDAO property of the above CombatService.java class.

6. Create the application Controller

Finally let’s create the controller class as CombatController.java in the package com.jCombat.controller.

CombatController.java

If we don’t specify the component-scan element in application context (dispatcher-servlet.xml), we would then need to define the beans in the context XML (dispatcher-servlet.xml) before we would have been able to autowire them. Since we are using the component-scan in our application here, we didn’t have to specify the bean for each of the classes, except for the DataSource, which references the class from the Apache API.

Important points while trying out this demo

This was all, but do make sure that the following points have been taken care of to avoid unexpected errors –

  1. Make sure you have created table “login” in the database with two columns “uname” and “password” of type varchar2 and provide some values in it.
  2. Before executing this project, do make sure the database engine is running otherwise you will get connection errors.

7. Project Execution

Now you can execute your project by right-clicking your project in Eclipse and choose Run as->Run on server.

Below are the browser window snapshots when the application is run and we do a basic navigation.

7.1 Landing page

Landing Page - Spring with Jdbc template

7.2 Login page

Clicking on the login link, the application navigates to the login page as shown below –

2

7.3 Check validation

Click on the login button without entering anything into any of the textboxes. We get an error message as –

3

Enter any wrong credentials and click on the login button, we get an error message as shown below –
4

7.4 Successful login

Now login with the correct credentials (username – test and password – 123), we get logged-in successfully to the user page as shown below –

6

I hope this post was fair enough. Do comment for your queries and feedback on the same.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.