| Author |
load date from db and add to combo box
|
robin singal
Ranch Hand
Joined: Jul 18, 2007
Posts: 102
|
|
Hi I want to populate a combo box with the dates which i would do by invoking a servlet. My questions are: 1) How can i load a servlet as soon as the Jsp page loads as former would pick up values from database and add one more date each day to db. 2) How can i add the option tag dynamically as there would be hundred's of them which would obviously keep on increasing, as each day there would be an increase in the option tag (as the combo box shall contain the dates); I searched a lot but could not find the exact solution to my problem so came to javaranch. Thanks Robin
|
Life is short play hard.
|
 |
Ashok Kumar Babu
Ranch Hand
Joined: Jul 25, 2006
Posts: 129
|
|
Hi,
How can i load a servlet as soon as the Jsp page loads as former would pick up values from database and add one more date each day to db.
. It depends: 1. If you dont have any data from the Jsp while calling the servlet: Then you can directly call the JSP from the Servlet, do whatever you want with db in that servlet itself and send those results to the jsps as attributes. 2. But if you have some data with which the servlet works. Then as you asked. You can call a Java script method when your Jsp page loads and from that JavaScript, make a JavaScript request to the Servlet. Ajax is the Key.
|
Ashok<br /> <br />SCJP 91%<br />SCWCD 88%
|
 |
robin singal
Ranch Hand
Joined: Jul 18, 2007
Posts: 102
|
|
Thanks for response I got the solution for my first question but solution to 2nd one is still pending. As one just add the <option> tag in select tag add an entry to the combo box. I want the value that is returned to the jsp as a array list which inturn contains dates to be displayed in the combo box. Now how could i generate the <option> tags equal to the length of the array list and display each array list values. Thanks
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
First, see: http://faq.javaranch.com/java/HtmlHasNoComboBox JSPs produce HTML (usually) and there is no combo box in HTML. I'm not sure what you mean by 'load a servlet as soon as the jsp loads'. If you're using a servlet as a controller, it should instanciate whatever class fetches the information from the database (dates or anything else), and bind that data to scope. It should then forward context to your JSP which can loop through the data structure to build the option tags for your select list. This is pretty standard. Maybe I'm not understanding your question.
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Ashok Kumar Babu
Ranch Hand
Joined: Jul 25, 2006
Posts: 129
|
|
I want the value that is returned to the jsp as a array list which inturn contains dates to be displayed in the combo box. Now how could i generate the <option> tags equal to the length of the array list and display each array list values.
That is pretty simple. In your <select> tag (ComboBox), Iterate the arraylist which you have in the Jsp and just insert the <option> tag in the arraylist iterate loop.
|
 |
robin singal
Ranch Hand
Joined: Jul 18, 2007
Posts: 102
|
|
Thanks for reply. Actually the question i have asked is really pretty standard as i am new to developing webapp. I implemented tha approach you just told and successfully created the dropdown list (read the link you provided). Thanks for the answer. One more thing regarding I'm not sure what you mean by 'load a servlet as soon as the jsp loads'. Actually i was thinking if it is possible to load a servlet, when a jsp page is loaded i.e if the jsp page is the first page of the website & one have to fetch values from the database which would be displayed on that page then i would like to call the servlet as soon as one hits the url(or when the jsp page loads) eg. javaranch.com and the servlet would return the values to the welcome page or the first page. How would i invoke the servlet as soon as the jsp loads so that the values from database be displayed on that first page. Thanks.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56187
|
|
|
You may benefit from reading this article on web app structure.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
 |
|
|
subject: load date from db and add to combo box
|
|
|