Login Registration and Logout in Spring MVC with Hibernate

In this article, we will learn How to create a Registration, Login, and logout application in Spring MVC with Hibernate. If you want to learn Spring MVC, do check this article https://codedec.com/tutorials/simple-example-of-spring-mvc/

Login Registration and Logout in Spring MVC with Hibernate

We will develop the Registration, Login, and logout application using Spring MVC Framework, Hibernate Framework at the Data access layer, and MYSQL as a database. If you want to learn more about Hibernate Framework check this article https://codedec.com/course/hibernate-tutorial/

The following are the tools and technologies we will use:

  • Core java
  • Spring MVC
  • Mysql Database
  • Spring STS
  • Tomcat Apache Server

Create a Database

The first step is to create a database name ‘db_demo’ using the MYSQL command line or Workbench.

Create database db_demo

Create a Project in Spring STS

Create a Spring Project Go to File> New > Other > Search maven > Select Maven Project > Next > Search Filter org.apche.maven.archetypes/webapp > Next > Enter Group Id & Archetype id > Finish.

The project structure after adding all the files should look like the following:

Add the following dependencies into the pom.xml files

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Topic</groupId>
  <artifactId>RegAndLoginWithHb</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>RegAndLoginWithHb Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
  <springframework.version>5.0.6.RELEASE</springframework.version>
  <hibernate.version>5.4.1.Final</hibernate.version>
  <mysql.connector.version>8.0.17</mysql.connector.version>
  <c3po.version>0.9.5.2</c3po.version>
  <springsecurity.version>5.0.3.RELEASE</springsecurity.version>

  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
 </properties>

 <dependencies>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${springframework.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
   <version>${springframework.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-orm</artifactId>
   <version>${springframework.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-web</artifactId>
   <version>${springsecurity.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-config</artifactId>
   <version>${springframework.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>${springframework.version}</version>
  </dependency>

  <!-- Add Spring Security Taglibs support -->
  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-taglibs</artifactId>
   <version>${springsecurity.version}</version>
  </dependency>

  <!-- Add Jackson for JSON converters -->
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.9.5</version>
  </dependency>

  <!-- Hibernate -->
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>${hibernate.version}</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-validator</artifactId>
   <version>6.1.3.Final</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator-cdi -->
  <dependency>
   <groupId>org.hibernate.validator</groupId>
   <artifactId>hibernate-validator-cdi</artifactId>
   <version>6.1.3.Final</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
  <dependency>
   <groupId>javax.validation</groupId>
   <artifactId>validation-api</artifactId>
   <version>2.0.1.Final</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator-annotation-processor -->
  <dependency>
   <groupId>org.hibernate.validator</groupId>
   <artifactId>hibernate-validator-annotation-processor</artifactId>
   <version>6.1.3.Final</version>
  </dependency>


  <!-- MySQL -->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>${mysql.connector.version}</version>
  </dependency>

  <!-- Servlet+JSP+JSTL -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
  </dependency>

  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>javax.servlet.jsp-api</artifactId>
   <version>2.3.1</version>
  </dependency>

  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>

  <dependency>
   <groupId>javax.servlet.jsp.jstl</groupId>
   <artifactId>javax.servlet.jsp.jstl-api</artifactId>
   <version>1.2.1</version>
  </dependency>
  <dependency>
   <groupId>taglibs</groupId>
   <artifactId>standard</artifactId>
   <version>1.1.2</version>
  </dependency>

  <!-- to compensate for java 9 not including jaxb -->
  <dependency>
   <groupId>javax.xml.bind</groupId>
   <artifactId>jaxb-api</artifactId>
   <version>2.3.0</version>
  </dependency>

  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
  
  </dependencies>  
</project>

Configure Web.xml file

 Web.xml is a deployment descriptor that is used by the server to map the incoming request.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
  <servlet-name>SpringDemo</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/SpringDemo-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>SpringDemo</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

Create a SpringDemo-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

 <tx:annotation-driven />
 <!-- Adding Support for Component Scan -->
 <context:component-scan
  base-package="com.codebun" />
 <!-- Add View Resolver -->
 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/view/" />
  <property name="suffix" value=".jsp" />
 </bean>
 <!-- Create Data Source -->
 <bean
  class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  name="ds">
  <property name="driverClassName"
   value="com.mysql.jdbc.Driver" />
  <property name="url"
   value="jdbc:mysql://localhost:3306/db_demo" />
  <property name="username" value="root" />
  <property name="password" value="root" />
 </bean>
 <!-- Local SessionFactory -->
 <bean
  class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
  name="sessionFactory">

  <!-- data source -->
  <property name="dataSource" ref="ds"></property>

  <!-- hibernate properties -->
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
   </props>
  </property>

  <!-- annotated classes -->
  <property name="annotatedClasses">
   <list>
    <value>com.codebun.model.Student</value>
   </list>
  </property>

 </bean>

 <bean
  class="org.springframework.orm.hibernate5.HibernateTransactionManager"
  name="transactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <!--Add configuration of transactional behavior based on annotations -->
 <tx:annotation-driven
  transaction-manager="myTransactionManager" />
</beans>
  • Here, we have used <context:component-scan> detects the annotation by package scanning.
  • To resolves views such as JSP, we have used InternalResourceViewResolver.
  • Initialize and configure the DataSource. Specify Database connection properties here.
  • Add the hibernate properties and add the Student.java bean.

Create a Model class

Create a Model class Student.java that will contain attributes with setters and getters inside the model package.

package com.codebun.model;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "student_table")
public class Student {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private int id;
 private String name;
 private String userName;
 private String city;
 private String state;
 private String country;
 private String email;
 private String address;
 private String password;
 
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public Student() {
  super();
 }
 public Student(int id, String name, String city, String state, String country, String email, String address, String password, String userName) {
  super();
  this.id = id;
  this.name = name;
  this.userName = userName;
  this.city = city;
  this.state = state;
  this.country = country;
  this.email = email;
  this.address = address;
  this.password = password;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
 public String getState() {
  return state;
 }
 public void setState(String state) {
  this.state = state;
 }
 public String getCountry() {
  return country;
 }
 public void setCountry(String country) {
  this.country = country;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 
 
}
  • The @Entity annotation specified that the class is an Entity class.
  • @Table annotation is used to specify the name of the table that should be mapped with entities
  • @Id annotation is used for the primary key.

Create a DAO class

Create a Student interface and class to access data from the MYSQL database inside com.codebun.dao package.

StudentDAO.java

package com.codebun.dao;

import com.codebun.model.Student;

public interface StudentDAO {

 public void registerStudent(Student student);
 
 public Student loginStudent(Student student);
}

StudentDAOImpl.java

package com.codebun.dao;

import java.util.List;

import javax.persistence.NoResultException;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.codebun.model.Student;
@Repository
@Transactional
public class StudentDAOImpl implements StudentDAO{

 @Autowired
 private SessionFactory factory;
 @Override
 public void registerStudent(Student student) {
  // TODO Auto-generated method stub
  Session session = factory.getCurrentSession();
  session.save(student);
 }

 @Override
 public Student loginStudent(Student student) {
  // TODO Login
  Session session = factory.getCurrentSession();
  try {
  Query<Student> query = session.createQuery("from Student where userName =:userName and password =:password",Student.class);
  query.setParameter("userName", student.getUserName());
  query.setParameter("password", student.getPassword());
  student = query.getSingleResult();
  return student;
  }catch (NoResultException e) {
   // TODO: handle exception
   return null;
  }
 }
}
  • The registerStudent() method saves the data into the database.
  • The loginStudent() method authenticates user input with the input present in the database table and returns us the Student object.

Create a Service Class

Create a Student Service interface and class to perform business logic inside com.codebun.service package. Here, we have autowired the StudentDAO interface.

StudentService.java

package com.codebun.service;

import com.codebun.model.Student;

public interface StudentService {

 public void registerStudent(Student student);
 
 public Student loginStudent(Student student);
}

StudentServiceImpl.java

package com.codebun.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codebun.dao.StudentDAO;
import com.codebun.model.Student;
@Service
public class StudentServiceImpl implements StudentService {

 @Autowired
 private StudentDAO studentDao;
 @Override
 @Transactional
 public void registerStudent(Student student) {
  // TODO Auto-generated method stub
  studentDao.registerStudent(student);
 }

 @Override
 public Student loginStudent(Student student) {
  // TODO Auto-generated method stub
  return studentDao.loginStudent(student);
 }

}

Create a Controller class

Create a StudentController.java inside com.codebun.ctl package and annotate it with @Controller. The request for the web page will be handle by the handler methods in the controller class using @GetMapping and @PostMapping.

package com.codebun.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import com.codebun.model.Student;
import com.codebun.service.StudentService;

@Controller
public class StudentController {
 @Autowired
 private StudentService service;

 @GetMapping("/")
 public ModelAndView home(Model m) {
  Student student = new Student();
  m.addAttribute("student", student);
  ModelAndView modelAndView = new ModelAndView("register");		
  return modelAndView;		
 }
 @PostMapping("/register")
 public String register(@ModelAttribute ("student") Student student, Model model) {
  service.registerStudent(student);
  model.addAttribute("success","Registered Successfully");
  return "register";
 }
 @GetMapping("/login")
 public String loginDisplay(Model m,HttpSession session) {
  
  Student student = new Student();
  
  if (session.getAttribute("student") != null) {
   session.invalidate();
   System.out.println("here");
   m.addAttribute("success", "You have logout Successfully!!!");
  }
  m.addAttribute("student", student);	
  return "login";		
 }
 @PostMapping("/login")
 public String login(@ModelAttribute ("student") Student student, Model model, HttpSession session) {
  Student student2 = service.loginStudent(student);
  System.out.println("student2"+student2);
  if(student2 != null) {
   System.out.println("hello");
   model.addAttribute("student",student2);
   session.setAttribute("student", student2);
   return "welcome";
  }
  if(student2 ==null) {
  System.out.println("on");
  model.addAttribute("error", "Invalid Credentials");}
  return "login";
  
 }
}
  • @Controller annotation marks the StudentController class a Request Handler.
  • Every request coming for the ‘/’ URL will be handled by the home() method. It would redirect you to the register page.
  • The post request for ‘/register’ is handled by the register() method and it will call the registerStudent() method of service class.
  • The post request for ‘/login’ is handled by the login() method and it will call the loginStudent() method of service class.
  • @ModelAttribute in register method will read data from the form.

Create Views

Inside WEB-INF create a folder view and all the JSP pages inside this folder. Let us create register.jsp, login.jsp and welcome.jsp.

register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js" integrity="sha384-eMNCOe7tC1doHpGoWe/6oMVemdAVTMs2xqW4mwXrXsW0L84Iytr2wi5v2QjrP/xp" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.min.js" integrity="sha384-cn7l7gDp0eyniUwwAZgrzD06kc/tftFf19TOAs2zVinnD/C7E91j9yyk5//jjpt/" crossorigin="anonymous"></script>
<meta charset="ISO-8859-1">
<title>Register</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-success">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">RegistrationAndLogin</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <a class="nav-link  active" aria-current="page" href='<c:url value="/login"></c:url>'>Login</a>
        </li>
        <li class="nav-item">
          <a class="nav-link  active" aria-current="page" href='<c:url value="/"></c:url>'>Register</a>
        </li>
      </ul>
     
    </div>
  </div>
</nav>
<div class="container mt-5">
<div class="card" style="width: 55rem; ">
  <div class="card-header text-center bg-light" >
    Registration
  </div>
  <c:if test="${success != null}">
 <div  style="color: green;font-size: 20px;" role="alert"><c:out value="${success}"/></div>
</c:if>
  <div class="card-body" >
   <f:form action="register" modelAttribute="student" method="post">
  <div class="form-group">
    <label for="exampleInputEmail1">Name</label>
    <f:input type="text" path="name" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"/>
  </div>
  <div class="form-group">
    <label for="exampleInputEmail1">UserName</label>
    <f:input type="text" path="userName" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"/>
  </div>
  <div class="form-group">
    <label for="exampleInputEmail1">Email</label>
    <f:input type="text" path="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"/>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <f:input type="password" path="password" class="form-control" id="exampleInputPassword1"/>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Address</label>
    <f:textarea type="text" path="address" class="form-control" id="exampleInputPassword1"/>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">City</label>
    <f:input type="text" path="city" class="form-control" id="exampleInputPassword1"/>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">State</label>
    <f:input type="text" path="state" class="form-control" id="exampleInputPassword1"/>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Country</label>
    <f:input type="text" path="country"  class="form-control" id="exampleInputPassword1"/>
  </div>
  
  <center><button type="submit" class="btn btn-primary">Register</button></center>
</f:form>
  </div>
</div>
</div>
</body>
</html>

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js" integrity="sha384-eMNCOe7tC1doHpGoWe/6oMVemdAVTMs2xqW4mwXrXsW0L84Iytr2wi5v2QjrP/xp" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.min.js" integrity="sha384-cn7l7gDp0eyniUwwAZgrzD06kc/tftFf19TOAs2zVinnD/C7E91j9yyk5//jjpt/" crossorigin="anonymous"></script>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-success">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">RegistrationAndLogin</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <a class="nav-link  active" aria-current="page" href='<c:url value="/login"></c:url>'>Login</a>
        </li>
        <li class="nav-item">
          <a class="nav-link  active" aria-current="page" href='<c:url value="/"></c:url>'>Register</a>
        </li>
       
      </ul>
     
    </div>
  </div>
</nav>
<div class="container mt-5">
<div class="card" style="width: 55rem;">
  <div class="card-header text-center bg-light" >
    Login
  </div>
  <c:if test="${success != null}">
 <div  style="color: green;font-size: 20px;" role="alert"><c:out value="${success}"/></div>
</c:if>
<c:if test="${error != null}">
 <div  style="color: green;font-size: 20px;" role="alert"><c:out value="${error}"/></div>
</c:if>
  <div class="card-body"  >
    
   <f:form action="login" modelAttribute="student" method="post">
  <div class="form-group">
    <label for="exampleInputEmail1">UserName</label>
    <f:input type="text" path="userName" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"/>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <f:input type="password" path="password" class="form-control" id="exampleInputPassword1"/>
  </div>
  
  <br/>
  <center><button type="submit" class="btn btn-primary"> Login</button></center>
</f:form>
  </div>
</div>
</div>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
     <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js" integrity="sha384-eMNCOe7tC1doHpGoWe/6oMVemdAVTMs2xqW4mwXrXsW0L84Iytr2wi5v2QjrP/xp" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.min.js" integrity="sha384-cn7l7gDp0eyniUwwAZgrzD06kc/tftFf19TOAs2zVinnD/C7E91j9yyk5//jjpt/" crossorigin="anonymous"></script>
<meta charset="ISO-8859-1">
<title>Welcome</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-success">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">RegistrationAndLogin</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    
      <ul class="navbar-nav ml-auto ">
        <li class="nav-item">
          <a class="nav-link  active" aria-current="page" href='<c:url value="/login"></c:url>'>Logout</a>
        </li>
       
      </ul>
     
  
  </div>
</nav><br/>
<h1>Welcome <c:out value="${student.name} "></c:out></h1>
</body>
</html>

Run the Application

Now, Deploy the application on the server and perform registration and login and see the following output.

In this way, we have created the registration, login, and logout application using Spring MVC and Hibernate Frameworks.