Solved

JSP - capture the value from the dropdown list

Posted on 2007-03-25
13
351 Views
Last Modified: 2013-11-24
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>      
====================================================================
0
Comment
Question by:LeanMoreTryMore
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 10

Assisted Solution

by:Jaax
Jaax earned 80 total points
ID: 18790866
Replace
 <select name="pickupLocation">
to
 <select name="pickupLocation" onChange="document.forms[0].submit();">

In the JSP end, you may retrieve the value as
<%
...
String selectedValue = request.getParameter("pickupLocation");
...
%>


0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 18790900
<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>      
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 18790903
ooops.. the else part needs to take the "selected" out of it

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

 <%}} %>
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 30

Accepted Solution

by:
Mayank S earned 90 total points
ID: 18793723
<% for (int j = 0;j < Constants.SR_STATUS.length;j++) {
                     String value = Constants.SR_STATUS[j];
                   
                 %>
                 <option value="<%=value%>" <%= value.equals(request.getParameter("pickupLocation") ? "selected=\"true\"" : "" %> ><%=value%></option>
                 
 <%} %>
0
 

Author Comment

by:LeanMoreTryMore
ID: 18804351
I got compilation error

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

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 80 total points
ID: 18804466
have you tried my peice of code? It does the same thing.. but its written more on the simple coding style?
0
 

Author Comment

by:LeanMoreTryMore
ID: 18804508
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>
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18804576
>> value.equals(request.getParameter("pickupLocation")

You're missing an ) there

value.equals ( request.getParameter ( "pickupLocation" ) )
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18804577
(so was I)
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 18804613
there is a missing parentheses in it...
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 18804616
Ooops..

Late again..:-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18804809
No problem :)
0
 

Author Comment

by:LeanMoreTryMore
ID: 18805066
I did try Kuldeepchaturved's sample code. It works. many thanks for that.
Thankyou for you guys..
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sumHeights  challenge 17 71
where is session ID cache stored 1 51
netstat -ano | find "8000" and taskkill /f /pid 2984 3 57
eclipse console opening separately 2 21
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question