保留提交的JSP表单数据 [英] Retaining the submitted JSP form data

查看:87
本文介绍了保留提交的JSP表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

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

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 Map wherein the parameter name is the key.

所以使用以下表格

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

和大致以下的servlet方法

and roughly the following servlet method

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);
}

您应该能够访问结果中的输入.jsp 如下

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

这很简单。不需要 jsp:useBean 和/或讨厌的scriptlet。

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

这篇关于保留提交的JSP表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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