简体   繁体   English

保留提交的JSP表单数据

[英]Retaining the submitted JSP form data

I am having a web form(JSP) which submits the data to different application, hosted on different server. 我有一个Web表单(JSP),它将数据提交到不同的应用程序,托管在不同的服务器上。 After submitting the form data, that application redirect back to same JSP page. 提交表单数据后,该应用程序重定向回相同的JSP页面。 Now, I want to save the entered the data. 现在,我想保存输入的数据。 What are the different approaches to retain the submitted data in web form. 保留Web表单中提交的数据的不同方法有哪些。 I would not prefer to store the data in DB or any file. 我不想将数据存储在数据库或任何文件中。

PS: I would like to retain the submitted form data when request again redirected to same JSP page. PS:我想在请求再次重定向到同一个JSP页面时保留提交的表单数据。 Therefore, user need not to re-enter the data. 因此,用户无需重新输入数据。 Like, data can be stored in Session or Request etc. 比如,数据可以存储在Session或Request等中。

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 . 最好的办法就是提交给你自己的servlet,这个servlet又在java.net.URLConnection帮助下在后台激发对外部web应用程序的另一个请求。 Finally just post back to the result page within the same request, so that you can just access request parameters by EL . 最后,只需回到同一请求中的结果页面,这样您就可以通过EL访问请求参数。 There's an implicit EL variable ${param} which gives you access to the request parameters like a Map wherein the parameter name is the key. 有一个隐式EL变量${param} ,它允许您访问请求参数,如Map其中参数名称是键。

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.jsp as 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:useBean and/or nasty scriptlets. 不需要jsp:useBean和/或讨厌的scriptlet。

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. 在JSP中,通常使用javabean来存储表单值,然后使用jsp:useBean标记来处理这种事情。 For example you would create the following javabean: 例如,您将创建以下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): 并在您的表单中引用它(null省略检查):

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

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. 如果我正确理解了问题(大“如果”那里),你有一个FORM有一个POST方法和一个直接指向远程服务器的ACTION。 When the user clicks "Submit" the browser isn't involving your host in that transaction, the data is going to the "ACTION" recipient. 当用户单击“提交”时,浏览器不涉及该事务中的主机,数据将转到“ACTION”收件人。 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. 这将使您的选项仅限于实现与远程服务的回调关系(可能超出您的控制),设置本地代理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? 我有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM