title: Spring MVC - passing variables from one page to anther
body: <p>I need help. I am working on a project where I have multiple pages and multiple forms; each page has one form. I just need to be able to pass values from one jsp to another. What should I do? </p>
  
  <p>I am new to Spring MVC. I am using spring 2.5.6.</p>
  
  <p>Here's my design: </p>
  
  <p>formPage1.jsp --> Controller1 --> formPage2a.jsp --> Controller2 needs val frm pg1 &amp; pg2a.
  formPage1.jsp --> Controller1 --> formPage2b.jsp --> Controller3 needs val frm pg1 &amp; pg2b.
  formPage1.jsp --> Controller1 --> formPage2c.jsp --> Controller4 needs val frm pg1 &amp; pg2c.</p>
  
  <p>As you can see above, formPage1.jsp can load either formPage2a, formPage2b or formPage2c. Based on the input provided in formPage1.jsp, it goes to the controller (which is an extension of SimpleFormController) and controller get the values entered by user = command object. </p>
  
  <p>I want to be able to use these command object values in either formPage2a, formPage2b or formPage2c when they are submitted to another controller. </p>
  
  <p>here's the current code: </p>
  
  <hr>
  
  <h2>formPage1.jsp:</h2>
  
  <pre><code>&lt;form:form method="post" commandName="gainLossRequest"&gt;
              &lt;form:errors path="*" cssClass="error"/&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;table&gt;
                              &lt;tr&gt;
                                  &lt;td&gt;&lt;h4&gt;Choose Client&lt;/h4&gt;&lt;/td&gt;
                                  &lt;td style="font-size: medium; font-family: Arial, bold; color: red"&gt;*&lt;/td&gt;
                              &lt;/tr&gt;
                          &lt;/table&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;form:select path="client"&gt;
                              &lt;form:option value="none" label="Select" /&gt;
                              &lt;form:option value="abc" label="abc" /&gt;
                              &lt;form:option value="def" label="def" /&gt;
                              &lt;form:option value="xyz" label="xyz" /&gt;
                          &lt;/form:select&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
  &lt;tr&gt;
                      &lt;td colspan="2"&gt;
                          &lt;input type="reset" value="Reset" /&gt;
                          &lt;input type="submit" value="Next" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
              &lt;/form:form&gt;
  </code></pre>
  
  <hr>
  
  <h2>Controller1.java</h2>
  
  <pre><code>public class TestController extends SimpleFormController {
  
      /** Logger for this class and subclasses */
      protected final Log logger = LogFactory.getLog(getClass());
  
      public TestController() {
          logger.info("entering TestController.constructor..");
          setCommandClass(UserPreference.class);
          setCommandName("userPreference");
      }
  
      public ModelAndView onSubmit(HttpServletRequest request,
              HttpServletResponse response, Object command, BindException errors)
              throws ServletException {
          logger.info("entering TestController.onSubmit all..");
  
          UserPreference userPreference = (UserPreference) command;
  
          ModelAndView view = null;
  
          if ("abc".equals(userPreference.getClient())) {
              GainLossRequest gainLossRequest = new GainLossRequest(userPreference);
              view = new ModelAndView("redirect:/test/gainLossRequest.htm",
                      "gainLossRequest", gainLossRequest);
                  } else if ("def".equals(userPreference.getClient())) {
              IncomingPositionsRequest incomingPositionsRequest = new IncomingPositionsRequest();
              view = new ModelAndView(
                      "redirect:/test/incomingPositionsRequest.htm",
                      "incomingPositionsRequest", incomingPositionsRequest);
          } else if ("xyz".equals(userPreference
                  .getClient())) {
              TaxStrategyRequest taxStrategyRequest = new TaxStrategyRequest();
              view = new ModelAndView("redirect:/test/taxStrategyRequest.htm",
                      "taxStrategyRequest", taxStrategyRequest);
          }
          }
  }
  </code></pre>
  
  <hr>
  
  <h2>formPage2a.jsp</h2>
  
  <pre><code>&lt;form:form method="post" commandName="gainLossRequest"&gt;
              &lt;form:errors path="*" cssClass="error"/&gt;
              &lt;table style="width: 60%"&gt;
                  &lt;tr&gt;
                       &lt;td&gt;Account Number (s):&lt;/td&gt;
                       &lt;td style="font-size: medium; font-family: Arial, bold; color: red"&gt;*&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          User Chosen Client: 
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;c:out value="${gainLossRequest.client}"/&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                                  &lt;tr colspan="2"&gt;
                                          &lt;td&gt;
                          &lt;input type="reset" value="Reset" /&gt;
                          &lt;input type="submit" value="Submit" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
  </code></pre>
  
  <hr>
  
  <h2>dispatcher servlet config</h2>
  
  <pre><code>&lt;!-- setupNew.jsp is the first jsp --&gt; 
  
  &lt;bean name="/test/setupNew.htm" class="chimeraweb.web.TestController"&gt;
          &lt;property name="sessionForm" value="true"/&gt;
          &lt;property name="commandName" value="userPreference"/&gt;
          &lt;property name="commandClass" value="chimeraweb.service.UserPreference"/&gt;
          &lt;property name="validator"&gt;
              &lt;bean class="chimeraweb.service.UserPreferenceValidator"/&gt;
          &lt;/property&gt;
          &lt;property name="formView" value="/test/setupNew"/&gt;
      &lt;/bean&gt;
  
  
  &lt;!-- gainLossRequest.jsp is the 2nd jsp where I want to display the values captured in the first jsp page --&gt;
  
      &lt;bean name="/test/gainLossRequest.htm" class="chimeraweb.web.SimpleGainLossController"&gt;
          &lt;property name="sessionForm" value="true"/&gt;
          &lt;property name="commandName" value="gainLossRequest"/&gt;
          &lt;property name="commandClass" value="chimeraweb.service.GainLossRequest"/&gt;
          &lt;property name="validator"&gt;
              &lt;bean class="chimeraweb.service.GainLossValidator"/&gt;
          &lt;/property&gt;
          &lt;property name="formView" value="/test/gainLossRequest"/&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <hr>
  
  <p>Please help!!</p>