Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsp form send data to servlet without changing page

Tags:

java

jsp

servlets

I use jsp+servlets and have a form. How can I send the form data to a servlet (to the doPost() method) without leaving the actual page, that contains the form?

I want to press the button "submit", the data should be sent and I want to still remain on the page with the form. I would rather not use javascript.

I have on http://localhost/myproject/

<form action="http://localhost/myproject/anotherpage" 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>

when clicking the submit button i get forwarded to the following page: http://localhost/myproject/anotherpage

but I want to stay on

http://localhost/myproject/

edit: right now I am going to write

request.getRequestDispatcher("/index.jsp").forward(request, response);

in the doPost() method

like image 340
Gero Avatar asked Jun 16 '15 07:06

Gero


1 Answers

You should have a form with method="POST" in your JSP

    <form method="post">
        <input type="number" name="number1" id="number1" />
        +
        <input type="number" name="number2" id="number2" />

        <input type="submit" />
    </form>

Then in your servlets, in the doPost method, you have to get the parameters of your form with getParameter("name"), do what you want on it, then resend it to your JSP (setAttribute). Don't forget to link with your jsp (last line of my example)

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String textNumber1 = request.getParameter("number1");
        String textNumber2 = request.getParameter("number2");
        int number1 = (!textNumber1.isEmpty() ? Integer.parseInt(textNumber1) : 0);
        int number2 = (!textNumber2.isEmpty() ? Integer.parseInt(textNumber2) : 0);
        int result = number1 + number2;

        request.setAttribute("result", Integer.toString(result));

        request.setAttribute("number1", Integer.toString(number1));
        request.setAttribute("number2", Integer.toString(number2));

        this.getServletContext().getRequestDispatcher("/WEB-INF/calc.jsp").forward(request, response);
    }

Finally on your JSP, get the attribute from your servlet you want to display on the same page with getAttribute

<%
    String number1 = (String) request.getAttribute("number1");
    String number2 = (String) request.getAttribute("number2");
    String result  = (String) request.getAttribute("result");

    if (number1 != null && number2 != null && result != null) {
        out.print(String.format("<p>Result of %s + %s = <strong>%s</strong></p>", number1, number2, result));
    }
%>

This example is a little calculator that show you the result of number1 + number 2 on the same page of the form ;)

like image 148
JulianF Avatar answered Oct 24 '22 10:10

JulianF