Form handling in Spring MVC using maven in eclipse environment: sending data from view to controller

Project Structure:

img
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mvc</groupId>
    <artifactId>springmvc</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>springmvc Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.13</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>springmvc</finalName>
    </build>
</project>

web.xml
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!-- configure dispatcher servlet -->
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-
        class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

RegisterController.java
package springmvc.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class RegisterController {
    @RequestMapping("/reg")
    public String register()
    {
        return "register";
    }
    @RequestMapping(path="/processform",method=RequestMethod.POST)
    public String handleForm(@RequestParam("email")String userEmail,

    @RequestParam("uname")String userName,
    @RequestParam("pass")String password,Model model)

    {
        System.out.println("user email is:"+userEmail);
        System.out.println("user name is:"+userName);
        System.out.println("user password is:"+password);
        model.addAttribute("email",userEmail);
        model.addAttribute("uname",userName);
        model.addAttribute("password",password);

        return "welcome";
    }
}

register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Registration Form</title>
    </head>
    <body>
        <!doctype html>
        <html lang="en">
        <head>
            <!-- Required meta tags -->
            <meta charset="utf-8">
            <meta name="viewport"
            content="width=device-width, initial-scale=1, shrink-to-fit=no">
            <!-- Bootstrap CSS -->
            <link rel="stylesheet"
            href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.c
            ss"
            integrity="sha384-
            Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
            crossorigin="anonymous">
            <title>Registration Form</title>
        </head>
        <body>
        <div class="container mt-5">
            <h2 class="text-center">Registration Form</h2>
            <form action="processform" method="post">
            <div class="mb-3">
                <label for="exampleInputEmail1" class="form-label">Email
                address</label> <input type="email"
                    class="form-control"
                    id="exampleInputEmail1"
                    name="email">
                <label for="exampleInputEmail1" class="form-label">
                UserName</label> <input type="text"
                    class="form-control"
                    id="exampleInputEmail1"
                    name="uname">
                <label for="exampleInputEmail1" class="form-label">
                Password</label> <input type="text"
                    class="form-control"
                    id="exampleInputEmail1"
                    name="pass">

            </div>
        </div>
        <div class="container text-center">
            <button type="submit" class="btn btn-primary">SignUp</button>
        </div>
        </form>
        <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
        integrity="sha384-

        KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"

        crossorigin="anonymous"></script>
        <script

        src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js"

        integrity="sha384-

        ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"

        crossorigin="anonymous"></script>
        <script

        src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js"

        integrity="sha384-

        JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"

        crossorigin="anonymous"></script>
    </body>
</html>

welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <%
        String name=(String)request.getAttribute("uname");
        String email=(String)request.getAttribute("email");
        String password=(String)request.getAttribute("password");
        %>
        <h2>user name is<%=name %></h2>
        <h2>user mail is<%=email %></h2>
        <h2>user password is<%=password %></h2>
    </body>
</html>

Output


img
img



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc








 Previous