Solved

JSP - capture the value from the dropdown list

Posted on 2007-03-25
13
357 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 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 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.

615 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