Link to home
Start Free TrialLog in
Avatar of LeanMoreTryMore
LeanMoreTryMore

asked on

JSP - capture the value from the dropdown list

I got the JSP page which calls itself where the Search button is clicked.
I don't know how to capture the value from the drop down list and pass back the value to the page itself.

For example, if the user select 'Perth', from the downdown list, it then triggers off the search fuction (all work - ejb function) and return all records with the location, Perth.
But in the dropdown list, i dont know how to place the selected value.
Because whenever the page calls itself, it resets the dropdown list.


====================================================================
              <td width="50" align="left" >
                <select name="pickupLocation">
                 <% for (int j = 0;j < Constants.SR_STATUS.length;j++) {
                     String value = Constants.SR_STATUS[j];
                 %>
                 <option value="<%=value%>"><%=value%></option>
                 <%  } %>
                </select>
              </td>      
====================================================================
SOLUTION
Avatar of Jaax
Jaax
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<td width="50" align="left" >
                <select name="pickupLocation">
                 <% for (int j = 0;j < Constants.SR_STATUS.length;j++) {
                     String value = Constants.SR_STATUS[j];
                    if(value.equals(request.getParameter("pickupLocation"));{
                 %>
                 <option value="<%=value%>" selected="true"><%=value%></option>
                 <% }else {%>
 <option value="<%=value%>" selected="true"><%=value%></option>

 <%}} %>
                </select>
              </td>      
ooops.. the else part needs to take the "selected" out of it

    <% }else {%>
 <option value="<%=value%>" ><%=value%></option>

 <%}} %>
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LeanMoreTryMore
LeanMoreTryMore

ASKER

I got compilation error

 <option value="<%=value%>" <%= value.equals(request.getParameter("pickupLocation") ? "selected=\"true\"" : "" %> ><%=value%></option>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kuldeepchaturvedi: - works
but i really like to use mayankeagle's way.
also thanks for Jaax's suggest

you all have my points but why i got compilation error..what is the correct syntax using the following

<option value="<%=value%>" <%= value.equals(request.getParameter("pickupLocation") ? "selected=\"true\"" : "" %> ><%=value%></option>
>> value.equals(request.getParameter("pickupLocation")

You're missing an ) there

value.equals ( request.getParameter ( "pickupLocation" ) )
(so was I)
there is a missing parentheses in it...
Ooops..

Late again..:-)
No problem :)
I did try Kuldeepchaturved's sample code. It works. many thanks for that.
Thankyou for you guys..