This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt 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 Introducing JavaFX 8 Programming this week in the JavaFX 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: 63334

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

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: dynamically populate drop-down box?
It's not a secret anymore!