Saving the submitted JSP form data

I have a web form (JSP) that submits data to another application hosted on different servers. After submitting the form data, this application redirects back to the same JSP page. Now I want to save the entered data. What are the different approaches to saving the submitted data in a web form. I would not want to store data in a database or any file.

PS: I would like to save the submitted form data when the request is redirected to the same JSP page again. Therefore, the user does not need to re-enter the data. For example, data may be stored in a session or request, etc.

+7
java jsp servlets
source share
3 answers

The best you can do is send your own servlet, which in turn raises another request for an external web application in the background with a little help from java.net.URLConnection . Finally, just send back to the results page within a single query so that you can simply access the EL query parameters. There is an implicit EL variable ${param} that gives you access to query parameters, such as a Map , where the parameter name is the key.

So, in the following form

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

and about 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 must have access to the input in result.jsp as follows

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

Just. No need for jsp:useBean and / or disgusting scripts.

+6
source share

In JSP, this view is usually processed using javabean to store form values, and then using the jsp: useBean tag. For example, you 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 jsp form, you would use the useBean tag, and your form field values ​​would get their bean values:

 <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 sent (then redirected back), you will have the following code that will push the published form data to the bean.

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

Another option is to simply fill out the form in a session on the save page:

 String var1 = request.getParameter("var1"); String var2 = request.getParameter("var2"); session.setAttribute("var1", val1); session.setAttribute("var2", val2); ... 

and refer to it in your form (zero check is omitted):

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

If I understand the problem correctly (the big “if” is there), you have a FORM that has a POST and ACTION method pointing directly to the remote server. When the user clicks "Submit", the browser does not use your host in this transaction, the data goes to the "ACTION" recipient. This will limit your ability to provide feedback to the remote service (possibly out of your control), configure a local proxy servlet to intercept data and forward the POST along with the designated recipient (which will cause the POST to appear from the server rather than client (this can lead to problems)) or use some AJAX design template to send form data in two places instead of one, which is determined by the FORM tag.

In addition, you will need to have some form of local storage, such as a database or file.

Did I even help?

+1
source share

All Articles