• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generating chained dynamic dropdowns using JSP/Servlet and AJAX

 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...

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
Posts: 6
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65217
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using jQuery and its .load() method reduces this to a single statement in the change handler.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic