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

populate a drop-down listbox

 
nitij kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
manav kher
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i am assuming you mean a listbox in html and from "previous listbox" you mean a listbox in the previous (not current) page...in 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>
<option><%=jspVar0%></option>
<option><%=jspVar2%></option>
........
....
</select>
</form>
or use javascript to add or edit items in a list box in the current page for eg
<script>
document.myform.myselect.options[0].text="sometext"(or <%=jspVar%>
</script>

manav
 
nitij kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="*"/>
</jsp:useBean>
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Rajesh
 
bredan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic