Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JSP - capture the value from the dropdown list

Posted on 2007-03-25
13
Medium Priority
?
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 10

Assisted Solution

by:Jaax
Jaax earned 320 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
LVL 30

Accepted Solution

by:
Mayank S earned 360 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 320 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
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 …
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.
Suggested Courses

670 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