I want to have two drop down menus where the first one is automatically populated upon visiting the page from a database column. When the user selects a value from that first drop down menu I want the second drop down menu to be populated depending from the database using that value.
I've heard there are ways to do this with ajax, php and javabeans but I also heard that the easiest way to do it is with just jsp variables(maybe session?) so when one value is picked in the first drop down menu the page is refreshed and that value is passed in as a parameter and the second drop down menu takes that value and updates the second drop down menu.
I am able to retrieve the values from the database using a ResultSet but from there I'm having a tough time figuring out how to populate the first drop down list and I'm also not sure how to reload the page with the selected value used as a parameter. Will I be using jsp to write HTML out to make the drop down menus?
Anyways any help would be greatly appreciated. Thanks in advance.
To answer the general question: Firstly, result set and JSP should never be used in the same sentence. Write model classes that do all the database access completely separated from any UI aspects. The data should be copied from the result set into Java collections/beans as appropriate, and the results sets closed as soon as possible.
Basically, the UI should know nothing about the database, and vice versa.
Controlling the action of the model class(es) is the job of the page controller. Once the controller gets the collections, is can place them onto the request as scoped variables and forward to the JSP page where JSTL/EL can take over the task of creating the HTML for the dropdowns.
Thanks for the quick response Bear. Yeah I'm not very familiar with standards such as having all database work separate from the UI although it does make sense as I guess I should always think of MVC. I've been using Dreamweaver and each page I produce is just one giant JSP, they don't separate anything, there is also no functions. All Dreamweaver produces is a bunch of java followed by some HTML.
Throw DreamWeaver away and use a real Java IDE. For example Eclipse or IntelliJ IDEA.
Develop code yourself. Do not drag'n'drop code before you actually understands what all that autogenerated code does and you can write it yourself without help of a visual editor.
Joined: Feb 05, 2009
I do use Eclipse and it is my favorite IDE. Just a few weeks ago I wrote my first servlet. I'm working with someone else though who has done most of this websites design using Dreamweaver and the stuff that he is unable to do I've been going into the code and fixing, this just happens to be one of the problems we've run into that Dreamweaver can't fix itself.
Joined: Oct 08, 2006
Fine. It was just a bad start. Hope you know better for the next time.
For the rest I don't have anything to add what Bear already mentioned. Separate model from view. Learn about the DAO pattern.