Spring MVC Example – Reading HTML Form Data

We have discussed earlier a simple example of Spring MVC in which we have created multiple view pages. Here, we are going to create another Spring MVC example in which we are going to read the HTML form data.

Following are the steps to create this example:

Directory Structure of Spring MVC example

Spring MVC Example – Reading HTML Form Data
  1. Create a web request page

In this step, we are going to create a web request page named index.jsp, which is providing three links to different view pages.


 <h2> Spring MVC Web application </h2>
 <a href = "home_page"> Home page | </a>
 <a href = "about_us"> About Us | </a>
 <a href = "general-form"> Query Form </a>
  • Create the Controller class

In this step, we are going to create the Controller class named MainController, which returns the JSP pages. We are going to map the view pages with the method using the @RequestMapping annotation.


 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 public class MainController {
     public String showForm() {
                 return "genform" ;          
     public String getFormData() {
                 return "formdata";
  • Add the entry of the Controller into web.xml file.

In this step, we are going to add the entry of the Controller in the web.xml file.


 <?xml version = "1.0" encoding = "UTF-8"?>
 <web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
                 xmlns = "http://xmlns.jcp.org/xml/ns/javaee"
                 xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee                    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
                 id = "WebApp_ID" version = "3.1">
                 <absolute-ordering />
                 <!-- Spring MVC Configs -->
                 <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
                 <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
  • Add the entry of bean into another XML file.

In this step, we are going to define the bean inside an XML file (usually spring-servlet.xml). We are going to keep our JSP files in a folder named view, which is available inside the WEB-INF folder. The "/WEB-INF/view/" is used as the prefix, and ".jsp" is used as the suffix for the JSP files.


 <?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"
                 xsi:schemaLocation = "  http://www.springframework.org/schema/beans
                 <!-- Step 3: Add support for component scanning -->
                 <context:component-scan base-package = "com.app.SpringMVC2" />
                 <!-- Step 4: Add support for conversion, formatting and validation support -->
                 <!-- Step 5: Define Spring MVC view resolver -->
                                 <property name = "prefix" value = "/WEB-INF/view/" />
                                 <property name = "suffix" value = ".jsp" />
  • Create the JSP files

In this step, we are going to create other JSP files that are required in the application. We are going to create two JSP files, one is genform, and the other is formdata. The genform.jsp file contains an HTML form, and formdata.jsp will get the form data and display it.


 <%@ page language = "java" contentType="text/html; charset=ISO-8859-1"
     pageEncoding = "ISO-8859-1"%>
 <!DOCTYPE html>
 <meta charset="ISO-8859-1">
 <title>Spring MVC Form</title>
 <form action = "processform" method = "get" >
  <input type = "text" name = "StudentName" placeholder = "Write your name "/>
  <input type = "submit" value = "Submit"/>


 <%@ page language = "java" contentType = "text/html; charset=ISO-8859-1"
     pageEncoding = "ISO-8859-1"%>
 <!DOCTYPE html>
 <meta charset="ISO-8859-1">
 <title>Insert title here</title>
 <h4>welcome ${param.StudentName} </h4>
 STUDENT NAME: ${param.StudentName}


Spring MVC Example – Reading HTML Form Data 1
Spring MVC Example – Reading HTML Form Data 2
Spring MVC Example – Reading HTML Form Data 3
Spring MVC Example – Reading HTML Form Data 4