• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Dependant drop down list boxes in JSP page

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Yup, yup, yup. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic