Here’s what I want to do. It seems simple, but I can’t get it to work. JSP1 – user fills out form, submits to JSP2. JSP2 populates the form values in a Bean and displays data, and offers user option to return and modify (history.back()), or submit to Servlet. I’ve come up with three different options, and each has problems.
OPTION 1: JSP1 – standard html form, submits to JSP2
<form name="testform" method="post" action="jsp2.jsp">
...
City: <input name="currentCity" type="text" />
JSP2 –
<jsp:useBean id="workorder" type="com.mycompany.app.WorkorderBean" class="com.mycompany.app.WorkorderBean" scope="request">
<jsp:setProperty name="workorder" property="*" />
</jsp:useBean>
...
currentCity: ${workorder.currentCity}
Problem -when JSP2 submits to controller, and I call WorkorderBean workorder = (WorkorderBean) request.getAttribute("workorder"); it returns null. So ‘scope=request’ doesn’t make it from JSP to servlet.
OPTION 2: Same scenario, but on JSP2 use ‘scope=session’.
Problem: when the user chooses to go back to JSP1 and modify data, then re-submits to JSP2, JSP2 does not use the new values, because it already has the bean as a session bean.
Question: Is there a way I can clear out the session bean when I submit from JSP1? I don’t think I can do this with Javascript.
OPTION 3: Have JSP1 submit to Servlet, which formats the session bean and sends to JSP2.
Problem: When user choose to go back from JSP2 to JSP1 to make changes, all data is lost in the form.
How can I make this work?
Possible solution for option 1:
In JSP2 put the request parameters in hidden form fields so that they can be submitted to your servlet. In the servlet you have to read the request parameters manually. But you need no session and you can go back from JSP2 to JSP1, change some values, submit to JSP2 again. Then a submit on JSP2 transfers the changed values to the servlet.
Update:
In your second option: The reason why the bean properties remains unchanged after a resubmit is, that you placed the
jsp:setPropertyTag inside ofjsp:useBean. With this constellationjsp:setPropertyis only called at bean creation. During the second call of JSP2 the beanworkorderalready exists in the session scope and nojsp:setPropertycall happens.You can change that behaviour if you place the
jsp:setPropertyTag outside ofjsp:useBean:Then
jsp:setPropertywill be called at every JSP2 call and overwrite the bean properties with the request parameters.