Loading
 
What's Next: View All Tutorial →

How to read and display the form data using request.getParameter and @Param attribute

In This article, you can learn how to read the user input in the forms field and display it Refer Spring MVC Hello World for base project setup.   1. Create a new applicationForm.jsp with some form input fields ApplicationForm ...

Published Date: 13/12/2014  Last Modified Date: 13/12/2014    New Post



In This article, you can learn how to read the user input in the forms field and display it

Refer Spring MVC Hello World for base project setup.
 

1. Create a new applicationForm.jsp with some form input fields


ApplicationForm jsp has three form element, to get input from the user

File Path: /Web Pages/WEB-INF/pages/applicationFrom.jsp
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Application Form</h1>
        <form name="appFrm" action="<%=request.getContextPath()%>/saveAndViewApplicationData" method="post">
            <table>
                <tr>
                    <td>Name:</td>
                    <td><input type="text" name="userName"/><td>
                </tr>
                <tr>
                    <td>Gender: </td>
                    <td>
                        <select name="gender">
                            <option value="M">Male</option>
                            <option value="F">Female</option>                
                        </select>
                    <td>
                </tr>
                <tr>
                    <td>Age Group? : </td>
                    <td><input type="checkbox" name="ageGroup"  value="Less than 30"/> Less than 30
                        <input type="checkbox" name="ageGroup"  value="Above 30"/> Above 30
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Submit"/></td>
                </tr>
            </table>

        </form>
    </body>
</html>
 

2. Create a new applicationFormData.jsp with some form input fields



ApplicationFormData is to display the data entered by the user

File Path: /Web Pages/WEB-INF/pages/applicationFromData.jsp
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Application Form</h1>
        <form name="appFrm" action="<%=request.getContextPath()%>/saveAndViewApplicationData" method="post">
            <table>
                <tr>
                    <td>Name:</td>
                    <td><input type="text" name="userName"/><td>
                </tr>
                <tr>
                    <td>Gender: </td>
                    <td>
                        <select name="gender">
                            <option value="M">Male</option>
                            <option value="F">Female</option>                
                        </select>
                    <td>
                </tr>
                <tr>
                    <td>Age Group? : </td>
                    <td><input type="checkbox" name="ageGroup"  value="Less than 30"/> Less than 30
                        <input type="checkbox" name="ageGroup"  value="Above 30"/> Above 30
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Submit"/></td>
                </tr>
            </table>

        </form>
    </body>
</html>
 

3. Create a new FormController( java class) 


FormController.java is used to create two requestMapping url

1. "/showApplicationForm" to display the application form to the use to get the data entered by the user
2. "/saveAndViewApplicationData" to reterive the value entered by the user and display in the output page
 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tutorial.web;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class FormController {

    @RequestMapping("/showApplicationForm")
    public String showApplicationForm(ModelMap map, HttpServletRequest request) {

        return "applicationForm";
    }

    @RequestMapping("/saveAndViewApplicationData")
    public String saveApplicationForm(ModelMap map, @RequestParam String userName, HttpServletRequest request) {

        //Using @RequestParam
        System.out.println("UserName using @RequestParam :: " + userName);

        //Using request.getParameter()
        System.out.println("UserName using request.getParamter :: " + request.getParameter("userName"));
        System.out.println(request.getParameter("gender"));
        System.out.println(request.getParameter("ageGroup"));

        //Setting data in the ModelMap object to display data in the jsp
        map.addAttribute("uName", request.getParameter("userName"));
        map.addAttribute("gndr", request.getParameter("gender"));
        map.addAttribute("age", request.getParameter("ageGroup"));

        return "applicationFormData";
    }

}

4. Run the Project


Hit url http://localhost:8084/SpringMVC/showApplicationForm

Fill the data and submit,

Html Output:

Application Form Data

Access using ModalMap attribute

Name: John
Gender: M
Age Group : Less than 30

Access using $ {param} attribute

Name: John
Gender: M
Age Group : Less than 30


Console output in server side:
 
UserName using @RequestParam :: John
UserName using request.getParamter :: John
M
Less than 30


5. Download Project

Spring MVC form handling
Awaiting for Administrator approval





Tags: Spring MVC

← Back to list


Related Post




×