Solved

JSP - capture the value from the dropdown list

Posted on 2007-03-25
13
352 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 65
looking for unix program to search some keywords in side EAR WAR and JAR files 10 100
hibernate jars 4 45
hibernate example for saving data 19 57
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

828 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