如何记住以前保存的表单数据以供后续请求使用 [英] How to remember previously saved form data for subsequent requests

查看:12
本文介绍了如何记住以前保存的表单数据以供后续请求使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 servlet (page1) 中有下面的代码,我想在按 Save 后转到第二个 servlet (page2),读取以 page1 形式编写的内容并将它们附加到一个单选组中,如下所示:

I have the code below in a servlet (page1) and I want after pressing Save to go to a second servlet (page2), read the content written in the form of page1 and append them in a radio group like this:

Question [i]: question  (i increases every time a question is added in page2)
radiobutton1 (radio1)
radiobutton2 (radio2)
radiobutton3 (radio3)

重点是我每次填写下面的表格时,都会在之前保存的数据下面添加数据.

The point is every time I fill the form below, the data shall be added below data that have been saved previously.

您能否为 servlet page2 推荐一些示例代码?

Could you suggest some sample code for servlet page2?

非常感谢.

out.println("<form  id="form1" action = "page2" method = "POST" >");            
        out.println("<input type="text" name="question"><br />");
        out.println("<input type="text" name="radio1"><br />");
        out.println("<input type="text" name="radio2"><br />");
        out.println("<input type="text" name="radio3"><br />");
        out.println("<input type = "submit" value = "Save">");

推荐答案

您可以使用 或会话范围来记住以前保存的数据.例如

You can use either <input type="hidden"> or the session scope to remember previously saved data. E.g.

<input type="hidden" name="question1answer" value="42" />

request.getSession().setAttribute("question1answer", 42);

传递的数据是后续可用的请求

The passed data is the subsequent request available as

String question1answer = request.getParameter("question1answer");

Integer question1answer = (Integer) request.getSession().getAttribute("question1answer");

隐藏输入的缺点是它会产生相当多的样板代码,最终用户可以很容易地猜测/操纵它.会话范围的缺点是它在同一会话中的所有请求之间共享(因此当最终用户在多个浏览器窗口/选项卡中打开同一页面时可能会干扰).为了结合两全其美,您可以在第一个请求时生成一个长而唯一的密钥,您可以将其用作将所有关联数据存储在会话范围内的密钥,并将该密钥作为隐藏请求参数传递.

The disadvantage of hidden inputs is that it produces quite some boilerplate code and that the enduser can easily guess/manipulate it. The disadvantage of the session scope is that it's shared across all requests within the same session (and thus may interfere when the enduser has the same page open in multiple browser windows/tabs). To combine best of both worlds, you can on 1st request generate a long and unique key which you use as a key to store all the associated data in the session scope and pass that key as hidden request parameter.

例如在第一个请求中

String key = UUID.randomUUID().toString();
request.setAttribute("key", key);
List<Answer> answers = new ArrayList<Answer>();
request.getSession().setAttribute(key, answers);
// ...
answer.add(question1answer);

在 HTML 中

<input type="hidden" name="key" value="${key}" />

以及所有后续请求

String key = request.getParameter("key");
request.setAttribute("key", key);
List<Answer> answers = (List<Answer>) request.getSession().getAttribute(key);
// ...
answers.add(question2answer); // question3answer, question4answer, etc.

这篇关于如何记住以前保存的表单数据以供后续请求使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆