Solved

JSP - capture the value from the dropdown list

Posted on 2007-03-25
13
348 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
 
LVL 30

Accepted Solution

by:
mayankeagle 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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:mayankeagle
ID: 18804576
>> value.equals(request.getParameter("pickupLocation")

You're missing an ) there

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

Expert Comment

by:mayankeagle
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:mayankeagle
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now