tags:

views:

77

answers:

1

Scenario:

  1. A user fills out my JSP form by selecting his/her state from a drop down list. (The drop down list is populated from a database)

  2. The user hits "Next" on my form to go to the next page.

  3. The user goes back to the previous page by hitting "Back" on my form.

Problem:

The State field does not repopulate with what the user selected.

Question:

How do you repopulate the field with what the user selected? (I tried using http sessions). I have included my code below:

Current Code:

   <td><select name="State" tabindex="6">
    <option>--</option>
    <% for(int i=0; i<stateDDL.size()-1; i++){ 
         String state = stateDDL.get(i).toString();
         i++;
         String stateVal = stateDDL.get(i).toString();%>
    <option value=<%=stateVal%>><%=state%></option>
    <%
         } %>
   </select></td>

Note*: stateVal represents the value that is being sent to the database (i.e. "AL") state represents the string the user sees in the drop down menu (i.e. "Alabama")

A: 

Use javascript with the back link of your 2nd page. Something like

<a href="history.back(-1);return false;">back</a>

I noticed the use of stateDDL list. You should define a class State having two attributes code and name with getter and setter getCode() and getName(). You can then create objects of State and put into the list. Your code will look like this in jsp.

<% for (int i = 0; i < stateDDL.size(); i++) {
    State state = stateDDL.get(i);
%>
    <option value=<%=state.getCode()%>><%=state.getName()%></option>
<% } %>
Bhushan