How to Capture Data using @RequestParam Annotation in Spring?
Last Updated :
30 Apr, 2025
The @RequestParam annotation enables Spring to capture input data that may be passed as a query, form data, or any arbitrary custom data. It is used to bind a web request parameter to a method parameter. Here, we are going to understand these two above lines, and we will see how we can capture data using this annotation.
Note: We are going to use Spring Tool Suite 4 IDE for this project. Please go through how to download and Install Spring Tool Suite (Spring Tools 4 for Eclipse) IDE.
Setting Up the Project
Let's create a simple MVC application and use the @RequestParam Annotation inside the application.
Step 1: Create a New Maven Project
1.1: Go to your STS IDE, then create a new maven project, File > New > Maven Project, and choose the following archetype as shown in the image below.
1.2: Configure the DispatcherServlet in the web.xml file.
XML
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Spring Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Project Structure
Let us have a look at the project structure before moving into the coding part via the below media as follows:
2.1: So at first create an src/main/java folder and inside this folder create a class named CalculatorAppIntilizer and put it inside the com.geeksforgeeks.calculator.config package and extends the AbstractAnnotationConfigDispatcherServletInitializer class.
Refer to the below image as follows:
And whenever you are extending this class, it has some pre abstract methods that we need to provide the implementation. Now inside this class, we have to just write two lines of code to Configure the Dispatcher Servlet. Before that, we have to create another class for the Spring configuration file. So, go to the src/main/java folder and inside this folder create a class named CalculatorAppConfig and put it inside the com.geeksforgeeks.calculator.config package.
CalculatorAppConfig.java:
package com.geeksforgeeks.calculator.config;
// Importing required classes
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
// Class
@Configuration
@ComponentScan(basePackages = "com.geeksforgeeks.calculator.controllers")
public class CalculatorAppConfig {}
And below is the complete code for the CalculatorAppIntilizer.java file.
CalculatorAppIntilizer.java:
This class will initialize the Dispatcher Servlet.
Java
// Java Program to Demonstrate CalculatorAppIntilizer Class
package com.geeksforgeeks.calculator.config;
// Importing required classes
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
// Class
public class CalculatorAppIntilizer
extends AbstractAnnotationConfigDispatcherServletInitializer {
// Getter method
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
// Getter method
// Registering the Spring config file
@Override
protected Class<?>[] getServletConfigClasses() {
Class aClass[] = { CalculatorAppConfig.class };
return aClass;
}
// Adding mapping URL
@Override
protected String[] getServletMappings() {
// Declaring and initializing custom string
String arr[] = { "/geeksforgeeks.org/*" };
return arr;
}
}
Step 3: Setup ViewResolver
Spring MVC is a Web MVC Framework for building web applications. In generic all MVC frameworks provide a way of working with views. Spring does that via the ViewResolvers, which enables you to render models in the browser without tying the implementation to specific view technology. Read more here: ViewResolver in Spring MVC. So for setting up ViewResolver go to the CalculatorAppConfig.java file and write down the code as follows:
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".html");
return viewResolver;
}
And below is the updated code for the CalculatorAppConfig.java file after writing the code for setting up the ViewResolver.
Updated CalculatorAppConfig.java:
This class will enable Spring MVC and set up the ViewResolver.
Java
// Java Program to Illustrate Updated Calculator App
// Configuration Class
package com.geeksforgeeks.calculator.config;
// Importing required classes
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
// Class
@EnableWebMvc
@Configuration
@ComponentScan(basePackages
= "com.geeksforgeeks.calculator.controllers")
public class CalculatorAppConfig {
// Setting up ViewResolver
@Bean public InternalResourceViewResolver viewResolver()
{
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".html");
return viewResolver;
}
}
Step 4: Create Controller
Go to the src/main/java folder and inside this folder create a class named AppController and put it inside the com.geeksforgeeks.calculator.controllers package.
AppController.java file:
package com.geeksforgeeks.calculator.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class AppController {
@GetMapping("/home")
public String showHomePage() {
return "welcome-page";
}
@GetMapping("/process-homepage")
public String showResultPage(@RequestParam("number1") String num1,
@RequestParam("number2") String num2,
Model model) {
model.addAttribute("number1", num1);
model.addAttribute("number2", num2);
return "result-page";
}
}
Step 5: Create a View
Now we have to create a view named "welcome-page" inside the WEB-INF/view folder with the .html extension
welcome-page.jsp:
HTML
<html>
<head>
</head>
<body>
<h1 align="center">Capture Data using @RequestParam Annotation in Spring</h1>
<hr/>
<form action="process-homepage" method="get">
<div align="center">
<p>
<label for="num1">Enter First Number : </label> <input type="text"
id="num1" name="number1" />
</p>
<p>
<label for="num2">Enter Second Number : </label> <input type="text"
id="num2" name="number2" />
</p>
<input type="submit" value="Capture" />
</div>
</form>
</body>
</html>
The view is looking like below:
So, here we want to put some values inside the label and we want to capture that value so that we can display that value on our next page after clicking on the Capture button. So how to do it? And here @RequestParam Annotation comes into the picture.
Step 6: Capture Data using @RequestParam Annotation
As we have written this line inside the welcome-page.jsp file
<form action="process-homepage" method="get">
So, we have to create a controller with the "process-homepage" endpoint. So now come to the AppController.java file again and write down the following code inside this file.
@RequestMapping("/process-homepage")
public String showResultPage(@RequestParam("number1") String num1,
@RequestParam("number2") String num2, Model model)
{
model.addAttribute("number1", num1);
model.addAttribute("number2", num2);
return "result-page";
}
So, in the above code snippet let's understand this line
@RequestParam("number1") String num1, @RequestParam("number2") String num2, Model model
Here, inside the @RequestParam, we have passed "number1" and we have also given the same name inside the welcome-page.jsp file on this line of code.
<label for="num1">Enter First Number : </label> <input type="text"
id="num1" name="number1" />
And in "String num1" we have stored that value inside the "num1" variable. And the same goes for "number2" also. But now we have to send that value to the second page and how to do it. And here, "Model" comes into the picture.
Tip: In order to know more about Model in Spring, refer to How to Create Your First Model in Spring MVC
Updated AppController.java file:
This class will handle requests and capture data using the @RequestParam annotation.
Java
// Java Program to Illustrate Updated App Controller Class
package com.geeksforgeeks.calculator.controllers;
// Importing required classes
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
// Class
@Controller
public class AppController {
// Method
@RequestMapping("/home") public String showHomePage()
{
return "welcome-page";
}
// Method
@RequestMapping("/process-homepage")
public String
showResultPage(@RequestParam("number1") String num1,
@RequestParam("number2") String num2,
Model model)
{
model.addAttribute("number1", num1);
model.addAttribute("number2", num2);
return "result-page";
}
}
Now, we have to create another view named "result-page" to display the captured values. So below is the code for the result-page.jsp file.
Create result-page.jsp
<html>
<head>
</head>
<body>
<h1 align="center">Capture Data using @RequestParam Annotation in Spring</h1>
<hr/>
<p>First Number is: ${number1}</p>
<p>Second Number is: ${number2}</p>
</body>
</html>
So, now we have done with the coding part. Let's run and test our application.
Step 7: Run the application
7.1: To run our Spring MVC Application right-click on your project > Run As > Run on Server. And run your application as shown in the below image as depicted below as follows:
7.2: After that use the following URL to run your controller
http://localhost:8080/simple-calculator/geeksforgeeks.org/home
Output:
Now, let us put some values inside the label and click on the 'Capture' button. Suppose here we have put 23 and 45 and whenever we click on the Capture button an URL is generated as below as follows:
http://localhost:8080/simple-calculator/geeksforgeeks.org/process-homepage?number1=23&number2=45
And we can see on the next page the values are displayed and it is made possible via @RequestParam Annotation.
Similar Reads
Spring - How to Load Literal Values From Properties File
Literals in Java are a synthetic representation of boolean, numeric, character, or string data. It is a medium of expressing particular values in the program, such as an integer variable named ââ/count is assigned an integer value in the following statement. int x = 100; // Here 100 is a constant/li
5 min read
Spring MVC - Multiple View Page
A view page is redirected to another view page in this example. Let's look at a simple Spring Web MVC framework sample. The procedure is as follows: In the case of Maven, load the spring jar files or add dependencies.Make your controller class.Provide a controller entry in the web.xml file.In a sepa
3 min read
Spring MVC - Custom Validation
Validating user input is essential for any web application to ensure the processing of valid data. The Spring MVC framework supports the use of validation API. The validation API puts constraints on the user input using annotations and can validate both client-side and server-side. It provides stand
8 min read
Difference Between ApplicationContext and WebApplicationContext in Spring MVC
Spring MVC framework enables separation of modules namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain Java Classes. The model object can be passed between view and controller using maps. W
3 min read
Difference Between @Component, @Repository, @Service, and @Controller Annotations in Spring
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. Here, we are goi
4 min read
Difference Between @Controller and @Service Annotation in Spring
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. Spring @Control
5 min read
Difference Between @Controller and @RestController Annotation in Spring
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not directly affect the operation of the code they annotate. It does not change the action of the compiled program. Understanding the differ
3 min read
Spring MVC - @RequestParam Annotation
The @RequestParam annotation is one of the most commonly used annotations in Spring MVC for handling HTTP request parameters. @RequestParam annotation enables Spring to extract input data that may be passed as a query, form data, or any arbitrary custom data. Key features of @RequestParam annotation
5 min read
Query String and Query Parameter in Spring MVC
According to Wikipedia "A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters. A query string commonly includes fields added to a base URL by a Web browser or other client application, for example as part of an HTML, choosing the appearance of a pag
6 min read
How to Make Post Request in Java Spring?
Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using JAVA is that Java tries to connect every conc
4 min read