java jsp表单将数据发送到servlet而不改变页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30861456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:46:06  来源:igfitidea点击:

jsp form send data to servlet without changing page

javajspservlets

提问by Gero

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?

我使用 jsp+servlets 并有一个表单。如何在不离开包含表单的实际页面的情况下将表单数据发送到 servlet(发送到 doPost() 方法)?

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.

我想按下“提交”按钮,数据应该被发送,我想仍然留在带有表单的页面上。我宁愿不使用javascript。

I have on http://localhost/myproject/

我有 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

单击提交按钮时,我将转到以下页面: 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

在 doPost() 方法中

回答by JulianF

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

您的JSP 中应该有一个带有method="POST"的表单

    <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 doPostmethod, 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 linkwith your jsp (last line of my example)

然后在您的servlet 中,在doPost方法中,您必须使用getParameter("name")获取表单的参数,在其上执行您想要的操作,然后将其重新发送到您的 JSP ( setAttribute)。不要忘记与您的 jsp链接(我示例的最后一行)

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

最后,在您的JSP 上,从您的 servlet 中获取您想要使用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 ;)

这个例子是一个小计算器,它会在表单的同一页上显示 number1 + number 2 的结果;)

回答by Serge Ballesta

I would advise you not to forward to initial page from the second one, but instead to redirect to it in order to follow the Post-Redirect-Get pattern.

我建议您不要从第二个页面转发到初始页面,而是重定向到它以遵循Post-Redirect-Get 模式

If you do not, the user will see in his URL bar the address of the page where he posted data, and if he clicks the back button he will get the ugly message of duplicate post submissions.

如果你不这样做,用户将在他的 URL 栏中看到他发布数据的页面地址,如果他点击后退按钮,他将收到重复提交的丑陋消息。

So in you doPost()method just to :

所以在你的doPost()方法中只是为了:

response.sendRedirect("/back/to/your/page");

As as alternative, you could hidethe JSP page behind the servlet, and have the servlet to directly forward to the JSP page for a GET request, and do its workfor a POST and then either forward to the JSP or redirect to itself. In that case, you would not set any action in the <form>tag to have the data posted to same URL.

方法之一是,你可以隐藏在servlet背后的JSP页面,并有servlet来直接推进到一个GET请求JSP页面,并做其工作的POST,然后要么前进到JSP或重定向到自身。在这种情况下,您不会在<form>标签中设置任何操作来将数据发布到相同的 URL。

As it would keep same URL, you could eventually simply do a forward, but I still recommend a redirect to avoid the back button problem.

因为它会保持相同的 URL,你最终可以简单地做一个转发,但我仍然建议重定向以避免后退按钮问题。

回答by Sarit Adhikari

On you servlet, define the jsp you want to return.

在您的 servlet 上,定义要返回的 jsp。

RequestDispatcher requestDispatcher = req.getRequestDispatcher("/path/to/your/jsp");
    requestDispatcher.forward(req, res);

If you mean to send data without refreshing current page, you might want to look at sending ajax request.

如果您打算在不刷新当前页面的情况下发送数据,您可能需要查看发送 ajax 请求。

回答by dly

This doesn't let you stay on the form page, but redirects you back to it right away. And you can of course use the values you just entered.

这不会让您停留在表单页面上,而是立即将您重定向回该页面。您当然可以使用刚刚输入的值。

JSP form

JSP形式

<form method="post" action="yourServlet">

Servlet (in doPost())

小服务程序(中doPost()

getServletContext().getRequestDispatcher("/back/to/your.jsp").forward(request, response);

回答by Maurice Perry

You can use javascript and ajax (XMLHttpRequest), or specify a target to your form, that references a hidden iframe.

您可以使用 javascript 和 ajax (XMLHttpRequest),或为您的表单指定一个引用隐藏 iframe 的目标。