Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dependant drop down list boxes in JSP page

 
sherjeel ghouse
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William A White
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
sherjeel ghouse
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember, when comparing Strings, you must use .equals

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

-Yuriy
 
sherjeel ghouse
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic