Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retaining the submitted JSP form data

Tags:

java

jsp

servlets

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.

like image 304
Sourabh Avatar asked Jun 28 '09 17:06

Sourabh


People also ask

Which methods are used for reading form data using JSP?

JSP handles requests using getParameter() method to read simple parameters and getInputStream() method to read binary data stream coming from the client.

What is form processing in JSP?

There are various situations where we are ought to send some important information from browser to web server; and then web server to backend program. For example, when we fill a form, the browser takes that data, sends it over the web server and ultimately processes it to the backend program.

Can a JSP process HTML form data?

Reading Form Data using JSPJSP handles form data parsing automatically using the following methods depending on the situation: getParameter() − You call request. getParameter() method to get the value of a form parameter.


2 Answers

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

    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

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

like image 86
BalusC Avatar answered Oct 27 '22 02:10

BalusC


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") %>" />
like image 43
John Wagenleitner Avatar answered Oct 27 '22 00:10

John Wagenleitner