In Spring MVC, a POST request is used to send data from the client to the server, such as form data, registration details, login credentials, or JSON payloads. The server receives the submitted data, processes it, and returns an appropriate response.
- @PostMapping is used to handle HTTP POST requests in Spring MVC.
- POST requests are commonly used for creating or saving data on the server.
- Form data can be received using @ModelAttribute, while JSON data can be received using @RequestBody.
Handling Form Data Using @ModelAttribute
Used when data is submitted through an HTML form and needs to be bound to a Java object.
@PostMapping("/save")
public String saveData(@ModelAttribute User user) {
return "success";
}
- @ModelAttribute: Handles form data (application/x-www-form-urlencoded).
Handling JSON Data Using @RequestBody
Used when the client sends JSON data in the request body, commonly in RESTful web services.
@PostMapping("/save")
public String saveData(@RequestBody User user) {
return "Data Saved";
}
- @RequestBody: Handles JSON/XML data (application/json).
Post Request Processing Flow in Spring MVC
The following diagram illustrates how a request is processed in Spring MVC, from receiving the client request through the DispatcherServlet and Controller to generating and returning the final response to the client.

- Client (Postman) sends a POST request containing JSON data.
- DispatcherServlet receives the incoming request and identifies the appropriate Controller and forwards the request.
- HttpMessageConverter converts the incoming JSON data into a Java object using @RequestBody.
- Controller processes the request and performs the required business logic.
- Controller returns a response object or message.
- HttpMessageConverter converts the Java response into JSON (if required).
- DispatcherServlet sends the final response back to the client.
- Client (Postman) receives and displays the response.
Steps to Implement Form Submission using POST Request in Spring MVC
In this example, we will implement POST request handling using @ModelAttribute and JSP form submission.
Step 1: Create a Maven Project
- Open STS IDE.
- Click File - New - Maven Project
- Select Create a Simple Project and select Archetypes
- Click Next
Enter the following details:
- Group Id: com.gfg
- Artifact Id: SpringMVCPostRequest
- Packaging: war
Click Finish.
Step 2: Add Required Dependencies
Add Spring MVC, Servlet API, and JSTL dependencies to the pom.xml file.
<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.geeksforgeeks</groupId>
<artifactId>SpringMVCPostRequest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVCPostRequest Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</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>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>simple-calculator</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 3: Create Model Class
Go to src -> main -> java -> com.gfg.Spring.boot.app, create a java class with the name User Stores user information submitted from the JSP form.
package com.gfg.model;
public class User {
private String name;
private String email;
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Step 4: Configure DispatcherServlet
Configures DispatcherServlet and initializes the Spring MVC application.
package com.gfg.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class AppInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { AppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Step 5: Create Spring Configuration Class
Enables Spring MVC and configures View Resolver for JSP pages.
package com.gfg.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.gfg.controller")
public class AppConfig {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver =
new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
Step 6: Create Controller
Displays the form page and handles POST request submitted from the JSP form.
package com.gfg.controller;
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 com.gfg.model.User;
@Controller
public class UserController {
@GetMapping("/")
public String showForm(Model model) {
model.addAttribute("user", new User());
return "home";
}
@PostMapping("/save")
public String saveUser(
@ModelAttribute("user") User user,
Model model) {
model.addAttribute("user", user);
return "success";
}
}
Step 7: Create JSP Form Page
Collects user details and submits them using HTTP POST method.
<%@ taglib uri="http://www.springframework.org/tags/form"
prefix="form" %>
<html>
<head>
<title>User Form</title>
</head>
<body>
<h2>User Registration Form</h2>
<form:form action="save"
method="post"
modelAttribute="user">
Name:
<form:input path="name"/>
<br><br>
Email:
<form:input path="email"/>
<br><br>
<input type="submit" value="Submit"/>
</form:form>
</body>
</html>
Step 8: Create Result Page
Displays the submitted user information after successful form submission.
<html>
<head>
<title>Success</title>
</head>
<body>
<h2>Data Submitted Successfully</h2>
Name : ${user.name}
<br><br>
Email : ${user.email}
</body>
</html>
Step 9: Run the Application
- Right Click Project
- Run As - Run on Server
- Select Apache Tomcat
- Click Finish
Now open your Browser:
http://localhost:8080/SpringMVCPostRequest/
Output:

Now fill The Form To make a Post reguest from the Browser:

Once the Submit button is clicked the response comes as:

Explanation: In this application, the user enters data in a JSP form and submits it using the HTTP POST method. Spring MVC automatically binds the submitted form fields to a Java object using the @ModelAttribute annotation. The controller processes the data, stores it in the Model object, and returns a view that displays the submitted information to the user.