File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Dynamic Jsp coding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Dynamic Jsp coding" Watch "Dynamic Jsp coding" New topic

Dynamic Jsp coding

Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
Hi all,
I am facing a problem here..Please see if any one can help.
I jave a JSP page which has two drop down list boxes.
One list box is populated from the databse.
values: kishan
assuming all those were populated from database.
values: Chennai
should come if "kishan" is selected from the first listbox.
if "maha" is selected then
Values: Delhi
should be populated from the databse using the where condition
got by the first selection.
Both these boxes are in same page and i do not want to transfer
this to another page..staying in the same how to accomplish this.
An onChange function of the selection list on listBox1 can be
written which gives me what value is selected. This i can get in
Java script. But how to give this value to a jsp variable which
queries the database using the selected value.
Is there any other logic i can use.
remember there is no request that is passed. both are in same page and on selection it should stay in same page populating the
second one dynamically.
please help.

V. Kishan Kumar

Regards,<BR>V. Kishan Kumar
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
The only way I can think of is to pre-load all the possible values into JavaScript using the JSP data query when the page ie generated, then use the onChange as suggested to populate the second drop-down by selecting the appropriate items from the overall list.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
If I am not wrong, you don't want to go to the server to get the new set of options for the 2nd list whenever the uses selects an option from the 1st list right?. It is like selecting a country from first pulldown and show only those states for that selected contry in the 2nd pull down. Isn't? For this you have to have all possible data ready in hand.
What Frank said is the solution for this. Here is a piece of cake. The following url has the source code for simillar situation as yours. Enjoy!
Auto country-state selection source code using Java Script
maha anna
Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
Thanks for your reply..I got the point, but what amI going to do
in this scenario which Iam in.
What if the the database is very huge and keeping all the records
before hand from the second table is not a good design option,
Say there are 100 countries and 1,00,000 cities. The country names in one table and city name in another table then u might accept that keeping the 1 lakh records is not a good design, and
the user will just click one entry for the country which may contain only 15 cities..what ever it is a waste fetching
that many records while only 15 or 100 or 1000 maximum that are
So I am again to square 1 and how to proceed with the same conditions as before and add to that the above, just for idea sake please which is costly..
one database query fetching 1 lakh records
two database query fetching 100 and 10,000 records respectively
as per jsp and javascript coding is concerned.
One more small query.
Can a value of a javascript variable be assigned to a jsp variable. Is this possible.

V. Kishan Kumar
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Just in case you are unclear, it is vital to remember that JSP code and JavaScript code are executed at different times, on different machines, so it makes very little sense for them to communicate.
JSP code, just like servlet code, is executed only when building the HTML page to send to the browser. When the JSP code has completed, a page of HTML, JavaScript etc. is sent to the browser. Only once the browser has loaded the page it can start executing the JavaScript.
If you cannot send all your data with the initial page, then you either need to get another page with the correct data, or do something complicated with an applet which contacts the server for more data and communicates with JavaScript.
Going back to the server need not be as big a problem as you seem to think. Remember that the returned page can appear to the user as if it is the same page, but with the right data in it, for example, consider this simple JSP page (note that this has not been tested, and won't actually compile without a support class, but I hope you get the idea.
Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
You have to deal with novices like me who has not got concepts
into his mind.
Thanks very much for your clear explanation and really I am
amazed that in one jsp file itself this can be accomplished,
ofcourse it is called two times. It was easy to follow.
Thanks very much again for the effort and also be prepared for
more silly questions like these as i have just started

V. Kishan Kumar
subject: Dynamic Jsp coding
It's not a secret anymore!