Spring Boot Web/MVC, JSP, and JPA Integration with User Registration & Login Flows
Requirement: Creating a Project and implementing User Registration Flow.
Create a Spring Boot Project with Web and JPA Modules.
Note : I am using Oracle Database as part of JAP Module. So Please select respective Driver while creating application.
Add Database details and server port details inside application.properties.
server.port=9999
server.servlet.context-path=/tekteacher
#DB Properties.
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=c##dilip
spring.datasource.password=dilip
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
Now Add the Dependency of JSP inside the pom.xml file.
To support JSP functionalities by Spring Boot Embedded Tomcat server, we should add the below dependency.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
Now Add ViewResolver properties of JSP inside the application.properties file.
server.port=9999
server.servlet.context-path=/tekteacher
#DB Properties.
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=c##dilip
spring.datasource.password=dilip
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
#JSP
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
Create a view folder as per prefix value inside our Spring Boot application.
Create a JSP file for User Registration Form: user-register.jsp
<html>
<head>
<title> User Register</title>
</head>
<body>
<form action="user/register" method="POST">
Name : <input type="text" name="name" /> <br />
Email Id : <input type="text" name="email" /> <br />
Contact Number : <input type="text" name="contact" /> <br />
Password : <input type="password" name="pwd" /> <br />
<input type="submit" value="Register" /> <br />
</form>
</body>
</html>
Create another JSP file for User Registration Result Message, whether User Account Created or Not: result.jsp
<html>
<head>
<title> Result</title>
</head>
<body>
${message}
</body>
</html>
Create a DTO class for retrieving details from the HttpServeletRequest Object inside the Controller method.
package com.tek.teacher.dto;
public class UserReigtserDto {
private String name;
private String emailId;
private String contact;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Create Controller class and Methods for loading the User Registration Page and reading data from the Registration page. Once receive data at the controller side, we should store it inside the database.
Create Controller Class: UserController.java
package com.tek.teacher.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import com.tek.teacher.dto.UserReigtserDto;
import com.tek.teacher.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
@Controller
public class UserController {
@Autowired
UserService userService;
@GetMapping("register")
public String sayHello() {
return "register";
}
// User Register
@PostMapping("user/register")
public ModelAndView registerUser(HttpServletRequest request) {
//Extracting Data From HttpServletRequest to DTO
UserReigtserDto userReigtserDto = new UserReigtserDto();
userReigtserDto.setName(request.getParameter("name"));
userReigtserDto.setEmailId(request.getParameter("email"));
userReigtserDto.setContact(request.getParameter("contact"));
userReigtserDto.setPassword(request.getParameter("pwd"));
String result = userService.userRegistration(userReigtserDto);
ModelAndView modelAndView = new ModelAndView();
//setting result jsp file name
modelAndView.setViewName("result");
modelAndView.addObject("message", result);
return modelAndView;
}
}
Now Create a Service Layer class and respective method for storing User Information inside the database.
Create UserService Class: UserService.java
package com.tek.teacher.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tek.teacher.dto.UserReigtserDto;
import com.tek.teacher.entity.UsersInfo;
import com.tek.teacher.repository.UserRepository;
@Service
public class UserService {
@Autowired
UserRepository repository;
public String userRegistration(UserReigtserDto userReigtserDto) {
// convert dto instance to entity object
UsersInfo user = new UsersInfo();
user.setContact(userReigtserDto.getContact());
user.setEmailId(userReigtserDto.getEmailId());
user.setName(userReigtserDto.getName());
user.setPassword(userReigtserDto.getPassword());
repository.save(user);
return "User Registration Successfull.";
}
}
Now create a JPA Entity class for Database Operations, with columns related to User Details.
package com.tek.teacher.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table
public class UsersInfo{
@Id
@Column
private String emailId;
@Column
private String name;
@Column
private String contact;
@Column
private String password;
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Now create a JPA Repository Interface for UsersInfo entity class to make
Database operations. UserRepository.java
package com.tek.teacher.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.tek.teacher.entity.UsersInfo;
@Repository
public interface UserRepository extends JpaRepository<UsersInfo, String>{
}
Testing/Execution: Start Our Spring Boot Application
Now Access User Register Form: http://localhost:9999/tekteacher/register
This URL will load the Form for entering User Details as follows.
Now Enter User Information and then click on the Register button. Internally it will trigger another endpoint “user/register” from our controller.
Response :
Finally, User Information Successfully Stored Inside Database.
Logic For User Login Flow :
Create A JSP form for USer Login Details.
FileName: login.jsp
<html>
<head>
<title>Login User</title>
</head>
<body>
<form action="loginCheck" method="POST">
Email : <input type="text" name="email" /> <br />
Password : <input type="password" name="pwd" /> <br />
<input type="submit" value="Login" /> <br />
</form>
</body>
</html>
Now Create/add a Controller Method For Login Page/Form Loading.
Controller Class: UserController.java
// To load the Login Page
@GetMapping("login")
public ModelAndView loadLoginPage() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
}
Now Create anotherController Method For Receiving Login Form Data and validation of User Details.
Controller Class: UserController.java
@PostMapping("/loginCheck")
public ModelAndView validateUser(HttpServletRequest request) {
String result = userService.validateUser(request.getParameter("email"), request.getParameter("pwd"));
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("result");
modelAndView.addObject("message", result);
return modelAndView;
}
Create Another Method in Service Class to connect with Repository layer
Service Class: UserService.java
public String validateUser(String emailId, String password) {
// Verify in data base
List<UsersInfo> users = repository.findByEmailIdAndPassword(emailId, password);
if (users.size() == 0) {
return "Invalid Credentilas. Please Try again";
} else {
return "Welcome to FaceBook, " + emailId;
}
}
Now Create a custom findBy.. JPA method inside Repository Interface.
Interface: UserRepository.java
package com.tek.teacher.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.tek.teacher.entity.UsersInfo;
@Repository
public interface UserRepository extends JpaRepository<UsersInfo, String>{
List<UsersInfo> findByEmailIdAndPassword(String emailId, String password);
}
Testing : Load Login From.
Valid User Details : Enter Valid Email and Password
Response Page
InValid User Details : Enter InValid Email or Password
Response Page:
In the above, We have created a Spring Boot Web application with integration of the JSP layer and JPA layer for Database Operations.
All Project Source Code available in the Below Link.
Please follow for more stories and updates.