1
votes

I want create 2 JSP pages. I have Controller:

@Controller
@RequestMapping(value = "/api/web/users")
public class ServletWebController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "message");
        return "hello";
    }
}

It open first JSP page with form:

<html>
<body>
    <form action="main.jsp" method="post">
        First Name: <input type="text" name="first_name">
        <br />
        Last Name: <input type="text" name="last_name" />
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

When I input data to the form and press submit button, I want to open second JSP page and print data from form:

<html>
<head>
    <title>Using GET and POST Method to Read Form Data</title>
</head>
<body>
<center>
    <h1>Using GET Method to Read Form Data</h1>
    <ul>
        <li><p><b>First Name:</b>
            <%= request.getParameter("first_name")%>
        </p></li>
        <li><p><b>Last  Name:</b>
            <%= request.getParameter("last_name")%>
        </p></li>
    </ul>
</body>
</html>

But the second JSP page not opened. So I input http://localhost:4000/api/web/users And i have form. Then I input data and press "Submit". Then I have this link http://localhost:4000/api/web/main.jsp and error:

HTTP Status 404 - /api/web/main.jsp

type Status report

message /api/web/main.jsp

description The requested resource is not available.

Apache Tomcat/8.0.26

I am new in Spring and JSP, how can I open JSP from another JSP?

1
What's the jsp path for the newer one? Is it hello.jsp?Karthik R

1 Answers

0
votes

in your Controller you can do something like this:

@RequestMapping(value="/successpage", method =RequestMethod.POST)
public void successpage(ModelMap model, Users user)
{
    model.addAttribute("name", user.getFirstName());
}

@RequestMapping(value="/signupform", method=RequestMethod.GET)
public ModelAndView signupForm()
{
    return new ModelAndView("user", "command", new Users()); //Object that contains your getter and setters
}

On your form you can modify your open Form tag to look like this:

<form action="/successpage" method="POST">

In your WEB-INF/jsp folder you will need the 2 pages: signupform.jsp and successpage.jsp

After all is said and done, you can pass the ${name} variable on your successpage.jsp to see the value from the form.