• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamic Jsp coding

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
ListBox1:
values: kishan
maha
tony
assuming all those were populated from database.
ListBox2:
values: Chennai
Bangalore
Hyderabad
should come if "kishan" is selected from the first listbox.
if "maha" is selected then
Values: Delhi
Goa
Mumbai
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.

------------------
Regards,
V. Kishan Kumar
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kishan,
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
---------------------------------------------------------- http://javascript.internet.com/forms/country.html http://javascript.internet.com/forms/
regds
maha anna
 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maha,frank,
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..it is a waste fetching
that many records while only 15 or 100 or 1000 maximum that are
needed.
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.


------------------
Regards,
V. Kishan Kumar
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
country.jsp
 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frank,
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

------------------
Regards,
V. Kishan Kumar
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic