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

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

问题描述

我在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);

传递的数据是后续请求

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天全站免登陆