This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes dynamically populate drop-down box? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "dynamically populate drop-down box?" Watch "dynamically populate drop-down box?" New topic

dynamically populate drop-down box?

Annette L'Heureux
Ranch Hand

Joined: Dec 07, 2000
Posts: 135
I may be way off here, but is there a way to dynamically populate, using jsp, a drop-down box (a regular html form object)? I would need to access an oracle database, and pull up all the records in 1 column which would make up my drop-down list. I would need to dynamically do this, in case new records are ever added. Please tell me I'm not dreaming!!
Kevin Adams

Joined: Nov 12, 2000
Posts: 21
Here's what we do: I hope this answers your question.
Once you get a connection to the file you need, simply loop through the file and write out the options to the web page...

Annette L'Heureux
Ranch Hand

Joined: Dec 07, 2000
Posts: 135
Thanks Kevin, I definetly understand the idea! Now I'll just play around with it and see if I can get it to work. Do you put your code in a seperate servlet?
Thanks again!
Annette L'Heureux
Ranch Hand

Joined: Dec 07, 2000
Posts: 135
How do I get that code to run when a page is loaded?
The basic idea for me is that I will have some kind of link on the left, and it will load the new page on the right, but I need the drop-down box to be populated right away. So is there a way to get code to run on startup or something similar?
Darryl McCool

Joined: Jan 03, 2001
Posts: 8
The code in the JSP will be run as soon as you hit the JSP with your browser. So when the browser is ready to render the HTML in the JSP to the screen, the database has already been accessed and the <OPTION> tags have already been created from the data. The page is rendered with the drop-down box populated with this data.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
It is always good idea to minimize Java code in .jsp file. Especially dealing with DBConnection,database etc. Put those logic in a Java bean and keep it separate.
Please take a look at Frank's sample code in this discussion. You may get some idea.
maha anna
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60810

Sudhir Pawar,
Your post was moved to a new topic.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
subject: dynamically populate drop-down box?
Similar Threads
how to populate dropdown box dynamically
Javascript on html with file object
drop down list boxes
How to populate drop down box
Using a List for Field Population