The moose likes Servlets and the fly likes populate a drop-down listbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "populate a drop-down listbox" Watch "populate a drop-down listbox" New topic

populate a drop-down listbox

nitij kumar

Joined: Dec 08, 2000
Posts: 2
Is there an easy way using JSP/JavaBeans to populate a drop-down listbox based on a value that is chosen in a previous drop-down listbox?
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
If by "previous" you mean on a previous page, which has been submitted to a servlet/JSP, then it's fairly straightforward. If you mean another listbox on the same page then it's not a simple problem.
You might find the code examples at the following old thread helpful.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
manav kher
Ranch Hand

Joined: Nov 21, 2000
Posts: 98
well i am assuming you mean a listbox in html and from "previous listbox" you mean a listbox in the previous (not current) that case its pretty simple simply either use a jsp variable in html to set the value of the list box for eg
<form name="myform">
<select name="myselect" rows=1>
or use javascript to add or edit items in a list box in the current page for eg
document.myform.myselect.options[0].text="sometext"(or <%=jspVar%>

nitij kumar

Joined: Dec 08, 2000
Posts: 2
I would frame my query in a better way
I am using Javabeans say for example,
<jsp:useBean id="formHandler" class="foo.FormBean" scope="request">
<jsp:setProperty name="formHandler" property="*"/>
Now i need to set the getter/setter methods,
Here I access a textbox like this
public String getFirstName() {
return firstName;
//The Setter method
public void setFirstName(String fname) {
firstName =fname;
This particular piece of Info I retain to use in later pages,
Similarly How would i go about ListBoxes which has fixed values and once selected by user is retained/Used in later pages.
Warm Regards to all
Rajesh Hegde
Ranch Hand

Joined: Sep 15, 2000
Posts: 112
You can try this may be.
The listboxes LB1 & LB2 are in lets say demo.jsp. And u want to populate LB2 depending on the value in LB1. In the OnChange event handler for LB1 submit the form to demo.jsp itself by sending the selected value as data to the jsp. Then u can use the getparameter method within demo.jsp to populate the LB2 dynamically.
The whole point is about the jsp calling itself. I did a similar code recently and it worked perfect.

Joined: Dec 20, 2000
Posts: 11
In the JavaBean, I would use a string array to store the values of the dropdown listbox. In next page, you can retrieve the string array to populate the dropdown listbox
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Please note that The Java Ranch has a naming policy, described here and "bredan" is not a valid name. Please choose one which meets the requirements.
I agree. Here's the link:
subject: populate a drop-down listbox
It's not a secret anymore!