aspose file tools*
The moose likes JSP and the fly likes Dependant drop down list boxes in JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Dependant drop down list boxes in JSP page" Watch "Dependant drop down list boxes in JSP page" New topic
Author

Dependant drop down list boxes in JSP page

sherjeel ghouse
Greenhorn

Joined: Mar 29, 2005
Posts: 6
Hello

I have a JSP page with 2 text fields and two drop down list boxes. The first list box is pre populated by calling a collection object(data coming from database). When an item is selected from the first list box, the form is submitted to itself to populate the second list box. Now, the problem is it is populating the second list box, but the selection is first list box is lost, since the form is submitted to the server. I was able to handle the data lost in text fields by using combination of javascript and JSP's request object. I tried all different ways to save the state of first list box, but couldn't come up with a solution so far. I would really appreciate help with this.

thanks
sameer
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
Can't you send state of your box to the server in URL encoding and get the value when the page comes back? That's what I do.


All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
William A White
Ranch Hand

Joined: Jul 01, 2004
Posts: 35
When the form gets submitted, you save the selected value of the first list box.

When you redisplay the form, you check every element to see if it is equal to the saved value, and if it is you mark it SELECTED.
sherjeel ghouse
Greenhorn

Joined: Mar 29, 2005
Posts: 6
I did write the code to test the selection with existing list and marked as SELECTED, but still doesn't safe the state of the selection. Here is the code. I would appreciate any advice.

thanks

<select name="laboffice" onchange="findOwners()">
<option value="" > Select New Laboffice </option>

<%
MidasLabOfficeListClass _LabOfficeList = (MidasLabOfficeListClass) session.getValue("LabOfficeList");
for (int i = 0; i < _LabOfficeList.getLength(); i++) {
%>

<option value="<%= _LabOfficeList.getLabOffice(i).getLabOffice() %>"

<% if(request.getParameter("laboffice") == _LabOfficeList.getLabOffice(i).getLabOffice())
{
%>
SELECTED > <%= _LabOfficeList.getLabOffice(i).getLabOfficeDesc() %>
<%

}
else{ %>

> <%= _LabOfficeList.getLabOffice(i).getLabOfficeDesc() %>

<%

}

%>
</option>

<%} %>
</select>


</td>
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

You should really consider getting rid of all the scriptlets in your code. I have done what you are trying to do. I am using JSTL and JSP 2.0 EL.


[/code]


GenRocket - Experts at Building Test Data
sherjeel ghouse
Greenhorn

Joined: Mar 29, 2005
Posts: 6
Thanks for your advice, I really wanted to use JSP EL, but developers in my team are not familiar with it. This is an enhancement to existing software, so the managers wanted to use existing technology. I have the IF condition to test is the request parameter is equal to the collection populating the Drop down list. If this is true, that OPTION is set to SELECTED.But, for some reason, the code is displaying the last element in the list after a user selects anything.
I have included all my code. I would appreciate your help with this.

Thanks
sherjeel

<td>
<input type="hidden" name="which" value="0">
<select name="laboffice" onchange="findOwners()">

<%
MidasLabOfficeListClass _LabOfficeList = (MidasLabOfficeListClass) session.getValue("LabOfficeList");
for (int i = 0; i < _LabOfficeList.getLength(); i++)
{

%>

<% if(request.getParameter("laboffice") == _LabOfficeList.getLabOffice(i).getLabOffice())
{

%>

<option value="<%= _LabOfficeList.getLabOffice(i).getLabOffice() %>">
<%= _LabOfficeList.getLabOffice(i).getLabOfficeDesc() %>
</option>

<% }

else
{
%>

<option selected value="<%= _LabOfficeList.getLabOffice(i).getLabOffice() %>">
<%= _LabOfficeList.getLabOffice(i).getLabOfficeDesc() %>
</option>

<% } %>



<% } %>
</select>


</td>
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
remember, when comparing Strings, you must use .equals

if(_LabOfficeList.getLabOffice(i).getLabOffice().equals(request.getParameter("laboffice"))

-Yuriy
sherjeel ghouse
Greenhorn

Joined: Mar 29, 2005
Posts: 6
Well, i fixed it already. I did realize that i was not using the right string comparison operator. Anyways , thanks for your valuable advice. we came up with solution at the same time.

thanks

-sh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dependant drop down list boxes in JSP page
 
Similar Threads
JSP/JDBC problem...please help!!!!
not able to retained the value of Drop Down on a page sumbit.
Dropdown values for JSP
Text area and other contents are getting cleared on changing a drop down list
Reg : Drop-down box