0

I have 5 jsp pages with forms on each page. When I click on the submit button on each page the form is submitted to a servlet(Reg) and the values are stored in a class (bean). I use requestdispatcher to get the textfields from step1 in step2 and likewise step2 values in step3 and so on.

Now I want the values of the textfields dob and regDate (which are in my step1.jsp) in step4.jsp. How do I do that? Below is an example of the code I've written.

step1.jsp

<div>
<form name="step1">
<input type="text" name="dob" id="dob">
<input type="text" name="regDate" id="regDate">
<input type="submit" name="step1nxt" value="submit">
</form>
</div>

Step4.jsp

<div>
<form name="step4">
<input type="text" name="dod" id="dod">
<input type="submit" name="step4nxt" value="submit">
</form>
</div>

Reg.java ( Servlet )

if(step1nxt!=null){
        //call function to set values in bean
        request.getRequestDispatcher("step2.jsp").forward(request,response);
    }
    if(step2next!=null){
    //call function to set values
        request.getRequestDispatcher("step3.jsp").forward(request,response);
    }
    //similar 'if' statement for other pages
    if(step4.nxt!=null){
    //call function to set values
        request.getRequestDispatcher("step5.jsp").forward(request,response);
    }

bean.java

String dob;
    String regDate;
    //getters and setters
    //function to setvalues of dob and regDate
    //function to print the values
Abhiram mishra
  • 1,597
  • 2
  • 14
  • 34
Dell
  • 19
  • 8

1 Answers1

0

Instead of storing it in session scope you can use hidden fields in each form to pass the values to the next jsp. So step2 would look something like this

<div>
<form name="step2">
    <input type="hidden" value='<%=request.getParameter("dob")%>' name="dob" id="dob"/>
    <input type="hidden" value='<%=request.getParameter("regDate")%>' name="regDate" id="regDate"/>
    <input type="submit" name="step1nxt" value="submit">
</form>
</div>

Please note that using scriptlets in jsp is deprecated so you should consider using an MVC framework such as Struts2 or Spring MVC

Another way is to store the values in localStorage that's supported by all major browsers and you can store at least 5 Mb.

Or you could also use cookies

steven35
  • 3,747
  • 3
  • 34
  • 48
  • Thanks for the suggestion! I need the values in 2 pages though. Step2 as well as step4. – Dell Nov 27 '15 at 16:03
  • If you don't want to store them in session, then you will need to submit the values with each request. The easiest way to do this is to insert whatever you want to pass into the `
    ` so they are passed in the url as parameters (GET) or in the http headers (POST)
    – steven35 Nov 27 '15 at 16:07
  • I also edited my answer. I suggest you check out localStorage – steven35 Nov 27 '15 at 16:57
  • Yes I saw the edit. LocalStorage seems helpful to me. Thanks – Dell Nov 27 '15 at 17:23