Spring MVC Form Handling allows developers to capture user input from web forms, bind it to Java objects, and process it using controller logic. It simplifies handling form data by integrating model binding, validation, and view rendering in a structured way.
- Automatically maps form fields to Java objects using @ModelAttribute
- Easily validate user input and display errors in the view
- Clearly separates form logic (Controller), data (Model), and UI (View)
Steps to Create a Spring MVC Student Form in Eclipse IDE
A step-by-step guide to creating and handling forms in a Spring MVC application.
Step 1: Create Maven Web Project
Create a Maven project using maven-archetype-webapp in Eclipse/STS.
- Enter GroupId and ArtifactId
- Project is created with pom.xml and basic folder structure
- Packaging type should be WAR for web applications

Step 2: Add Required Dependencies (pom.xml)
Add Spring MVC and related dependencies in pom.xml.
- Add
spring-webmvc,spring-context,spring-core - Add
javax.servlet-apiandjsp-apifor servlet support - Add
jstlfor JSP tags. - Maven will automatically download and manage dependencies.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gfg</groupId>
<artifactId>SpringMvcStudentForm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.15</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMvcStudentForm</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using
Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see https://maven.apache.org/ref/3.9.11/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Step 3: Configure root-context.xml
Create root-context.xml inside /WEB-INF/spring/.
- Used to define shared beans across the application
- Keeps common configurations separate
- Can remain minimal for small projects
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans/
http://www.springframework.org/schema/beans//spring-beans.xsd">
</beans>
Step 4: Configure web.xml
- DispatcherServlet acts as front controller
- <url-pattern>/</url-pattern> handles all requests
- ContextLoaderListener loads Spring context at startup
- Links root-context.xml with the application
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html"
xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html
http://www.oracle.com/technetwork/java/index.html"
version="4.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>gfg</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>gfg</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
</web-app>
Step 5: Create Model Class (Student.java)
Create a POJO class representing form data.
- Fields: name, id, password
- Add constructors, getters, and setters
- Acts as data carrier between view and controller
package com.gfg.model;
public class Student {
private String name;
private String id;
private String password;
// needed to create a new instance via reflection by
// your persistence framework.
public Student() { super(); }
// if you don't create an constructor then there is no
// need to provide an empty constructor.
public Student(String name, String id, String password)
{
super();
this.name = name;
this.id = id;
this.password = password;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getPassword() { return password; }
public void setPassword(String password)
{
this.password = password;
};
@Override public String toString()
{
return String.format(
"Student [name=%s, id=%s, password=%s]", name,
id, password);
}
}
The gfg-servlet.xml file (in /WEB-INF) is named after the servlet in web.xml and configures Spring MVC. It enables annotations, scans controllers, handles resources, and resolves JSP views.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc/"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans/"
xmlns:context="http://www.springframework.org/schema/context/"
xsi:schemaLocation="http://www.springframework.org/schema/mvc/
http://www.springframework.org/schema/mvc//spring-mvc.xsd
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">
<resources mapping="/resources/**" location="/resources/" />
<annotation-driven />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
</beans:bean>
<context:component-scan base-package="com.spring.controller" />
</beans:beans>
Step 6: Create Controller Class (StudentController.java)
Create controller to handle requests.
- Annotate class with @Controller
- Acts as bridge between model and view
- Handles user input and business logic
package com.gfg.controller;
import com.gfg.model.Student;
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;
@Controller
public class StudentController {
@GetMapping(path = "/")
public String studentLogin(Model model)
{
model.addAttribute(
"var", "Please Enter Your Login Details");
return "login.jsp";
}
@PostMapping
public String
submitLogin(Model model,
@ModelAttribute("student") Student student)
{
if (student.getId() != null
& student.getPassword() != null) {
if (student.getId().equals("gfg")
&& student.getPassword().equals("123")) {
model.addAttribute("var",
student.getName());
return "success.jsp";
}
else {
model.addAttribute("error",
"Invalid Details");
return "login.jsp";
}
}
else {
model.addAttribute("error",
"Please enter Details");
return "login.js[";
}
}
}
Step 7: Handle GET Request (Load Form Page)
Define method using @GetMapping("/").
- Loads login page (login.jsp)
- Adds initial message to model
- Displays form to the user
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Login Form</title>
</head>
<body>
<form:form name="submitForm" method="POST">
<div align="center">
<table>
<tr>
<td>Student name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>User id</td>
<td><input type="text" name="id" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
<div style="color: red">${error}</div>
</div>
</form:form>
</body>
</html>
After successful login the StudentController class will redirect the page to success.jsp file located in "/src/main/webapp/WEB-INF/views/success.jsp". This page has a very simple HTML written welcoming page.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Success Form</title>
</head>
<body>
<font color="562789" align="center"><h1>Student Welcome page</h1></font>
<span>${var}</span> You have successfully logged in.
<font color="555555"><h1>Hope you have a great day !</h1></font>
</body>
</html>
After coding all classes and configuration file your project structure would look something like this:

Output:Â
Now It's time to run your project in tomcat, check out this link if you need help running a tomcat server. After successfully running the tomcat server type this link "http://localhost:8080/SpringMvcStudentForm/" in your favorite browser. (user id - "gfg" and password - "123")


So, we have created a very basic login form-based web application with Spring MVC and tested it locally in the tomcat server.