Java 保留提交的 JSP 表单数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1055308/
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-08-11 22:52:24  来源:igfitidea点击:

Retaining the submitted JSP form data

javajspservlets

提问by Sourabh

I am having a web form(JSP) which submits the data to different application, hosted on different server. After submitting the form data, that application redirect back to same JSP page. Now, I want to save the entered the data. What are the different approaches to retain the submitted data in web form. I would not prefer to store the data in DB or any file.

我有一个 Web 表单(JSP),它将数据提交到托管在不同服务器上的不同应用程序。提交表单数据后,该应用程序重定向回相同的 JSP 页面。现在,我想保存输入的数据。将提交的数据保留在 Web 表单中的不同方法有哪些。我不希望将数据存储在 DB 或任何文件中。

PS: I would like to retain the submitted form data when request again redirected to same JSP page. Therefore, user need not to re-enter the data. Like, data can be stored in Session or Request etc.

PS:当请求再次重定向到同一个 JSP 页面时,我想保留提交的表单数据。因此,用户无需重新输入数据。例如,数据可以存储在 Session 或 Request 等中。

回答by John Wagenleitner

In JSP this kind of thing is usually handled by using a javabean to store the form values and then using the jsp:useBean tag. For example you would create the following javabean:

在 JSP 中,这种事情通常是通过使用 javabean 来存储表单值,然后使用 jsp:useBean 标记来处理的。例如,您将创建以下 javabean:

package com.mycompany;
public class FormBean {
   private String var1;
   private String var2;
   public void setVar1(String var) { this.var1 = var; }
   public String getVar1() { return this.var1; }
   public void setVar2(String var) { this.var2 = var; }
   public String getVar2() { return this.var2; }
}

In your form jsp you'd use the useBean tag and your form fields values would get their values from the bean:

在您的表单 jsp 中,您将使用 useBean 标记,您的表单字段值将从 bean 中获取它们的值:

<jsp:useBean id="formBean" class="com.mycompany.FormBean" scope="session"/>
...
...
<input type="text" name="var1" value="<%=formBean.getVar1()%>" />

In your jsp the form data is posted to (then redirects back) you'd have the following code that would push the posted form data into the bean.

在您的 jsp 中,表单数据被发布到(然后重定向回来),您将有以下代码将发布的表单数据推送到 bean 中。

<jsp:useBean id="formBean" class="com.mycompany.FormBean" scope="session"/>
<jsp:setProperty name="formBean" property="*"/> 

Another option is to just stuff the form data into the session in your save page:

另一种选择是将表单数据填充到保存页面的会话中:

String var1 = request.getParameter("var1");
String var2 = request.getParameter("var2");

session.setAttribute("var1", val1);
session.setAttribute("var2", val2);
...

and reference it in your form (null checking omitted):

并在您的表单中引用它(省略空检查):

<input type="text" name="var1" value="<%= session.getAttribute("var1") %>" />

回答by BalusC

Best what you can do is to submit to your own servlet which in turn fires another request to the external webapplication in the background with little help of java.net.URLConnection. Finally just post back to the result page within the same request, so that you can just access request parameters by EL. There's an implicit EL variable ${param}which gives you access to the request parameters like a Mapwherein the parameter name is the key.

您能做的最好的事情是提交给您自己的 servlet,该 servlet 反过来在后台向外部 Web 应用程序发出另一个请求,几乎不需要java.net.URLConnection. 最后只需在同一个请求中回发到结果页面,这样您就可以通过EL访问请求参数。有一个隐式 EL 变量${param},它使您可以访问请求参数,例如 aMap其中参数名称是键。

So with the following form

所以用下面的形式

<form action="myservlet" method="post">
    <input type="text" name="foo">
    <input type="text" name="bar">
    <input type="submit">
</form>

and roughly the following servlet method

大致如下servlet方法

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    String foo = request.getParameter("foo");
    String bar = request.getParameter("bar");

    String url = "http://external.com/someapp";
    String charset = "UTF-8";
    String query = String.format("foo=%s&bar=%s", URLEncoder.encode(foo, charset), URLEncoder.encode(bar, charset));

    URLConnection connection = new URL(url).openConnection();
    connection.setUseCaches(false);
    connection.setDoOutput(true); // Triggers POST.
    connection.setRequestProperty("accept-charset", charset);
    connection.setRequestProperty("content-type", "application/x-www-form-urlencoded;charset=" + charset);

    try (OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), charset)) {
        writer.write(query);
    }

    InputStream result = connection.getInputStream();
    // Do something with result here? Check if it returned OK response?

    // Now forward to the JSP.
    request.getRequestDispatcher("result.jsp").forward(request, response);
}

you should be able to access the input in result.jspas follows

您应该能够result.jsp按如下方式访问输入

<p>Foo: <c:out value="${param.foo}" /></p>
<p>Bar: <c:out value="${param.bar}" /></p>

Simple as that. No need for jsp:useBeanand/or nasty scriptlets.

就那么简单。不需要jsp:useBean和/或讨厌的脚本。

回答by Jason Sperske

If I understand the problem correctly (big "if" there), you have a FORM that has a method of POST and an ACTION that is pointed directly to a remote server. When the user clicks "Submit" the browser isn't involving your host in that transaction, the data is going to the "ACTION" recipient. That would make your options limited to implementing a call back relationship with the remote service (possibly beyond your control), setting up a local proxy servlet to intercept the data and forward the POST along to it's intended recipient (which would make the POST originate from the server and not the client (this could likely cause problems)), or utilize some AJAX design pattern to send the form data to two places instead of just one which the FORM tag dictates.

如果我正确理解了问题(那里的大“如果”),则您有一个具有 POST 方法的 FORM 和一个直接指向远程服务器的 ACTION。当用户单击“提交”时,浏览器不涉及您的主机参与该事务,数据将发送到“ACTION”收件人。这将使您的选择仅限于实现与远程服务的回调关系(可能超出您的控制),设置本地代理 servlet 来拦截数据并将 POST 转发给它的预期接收者(这将使 POST 源自服务器而不是客户端(这可能会导致问题)),或者利用一些 AJAX 设计模式将表单数据发送到两个地方,而不仅仅是 FORM 标签指定的地方。

Beyond that, you would need to have some form of local persistence like a database or a file.

除此之外,您还需要某种形式的本地持久性,例如数据库或文件。

Did I help at all?

我有帮助吗?