• 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

Generating chained dynamic dropdowns using JSP/Servlet and AJAX

 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using jQuery and its .load() method reduces this to a single statement in the change handler.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic