tags:

views:

1434

answers:

3

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: 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.

+1  A: 

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:

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 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: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") %>" />
John Wagenleitner
+1  A: 

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.

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

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);
    OutputStreamWriter writer = null;

    try {
        writer = new OutputStreamWriter(connection.getOutputStream(), charset);
        writer.write(query);
    } finally {
        if (writer != null) { try { writer.close(); } catch (IOException e) {}
    }

    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

<p>Foo: ${param.foo}</p>
<p>Bar: ${param.bar}</p>

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

BalusC
+1  A: 

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.

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

Did I help at all?

Jason Sperske