aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Generating chained dynamic dropdowns using JSP/Servlet and AJAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Generating chained dynamic dropdowns using JSP/Servlet and AJAX" Watch "Generating chained dynamic dropdowns using JSP/Servlet and AJAX" New topic
Author

Generating chained dynamic dropdowns using JSP/Servlet and AJAX

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1000
    
    1

Just sharing the code/process to generate chain selector/cascading/dynamic dropdown using JSP/Servlet and AJAX. It might be helpful for peoples trying to implement this functionality.

Overview of my example

We have two tables in DB - Country and City.
Country has two columns - countryid and countryname
City has two column - cityid and cityname

we have requirement to create two dropdowns (one displaying all Country names and second one displaying city names corresponding to Country being selected in first dropdown ) on JSP page.

Functionality required is that - user select County name in first dropdown. Content of second dropdown should automatically populate corresponding list of cities that belongs to the Country selected.

here is the code

a. Create a jsp with first dropdown and code as shown below. Also include below shown ajax code in this jsp




Here is the ajax script that will fetch second dropdown from FetchCity servlet



Now in fetchCites servlet , in doGet()/doPost() method, call DB query (using JDBC/ Hibernate/any other ORM framework) to fetch all cities corresponding to country selected (countryid).
after that , prepare second dropwdown within servlet using out.println().

foreg - code generated for England selected as country.



this code will be placed on browser within second_dropdown_code div.

~ Abhay
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Thanks for sharing your work :)

Just out of curiosity, why aren't you using a modern JavaScript library? It would cut this down to just a few lines of code. (And please indent!)
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1000
    
    1

One reason that i can think off is Client restriction.
For eg - if we are developing page with this functionality for any client and that client has restriction of not to use any other jar/library/api
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

So you have clients that expect everything to be done in JSP, Servlets, and JavaScript, never using any libraries???

Get new clients.
rajpal b songara
Greenhorn

Joined: Nov 12, 2010
Posts: 6

David Newton...
can you give me which modern library i have to use to accomplish this task in few line...
and please give some real code...

thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Using jQuery and its .load() method reduces this to a single statement in the change handler.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generating chained dynamic dropdowns using JSP/Servlet and AJAX
 
Similar Threads
Drop Downs
Response from Servlet becomes null
correct Ajax script syntax for second DropDown
Selected value getting lost on form Submit
populate one drop down on change of other using ajax and servlet with jsp page