• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

updating drop down list boxes

Posts: 11
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok heres my problem..
Im creating a webpage (servlet).. and on that page, theres 2 drop down boxes.. BoxA and BoxB.. the contents of BoxB are dependant on the selection in BoxA.. so say you select a certain date in BoxA drop down list, BoxB drop down list will contain the names of people that visited the site that day. Select a different date in BoxA, the list in BoxB will dynamically change to reflect users on the new date. in other words, When User selects a listing in boxA, the servlet will query the database and populate boxb.
I havent written any code yet, and i was planning to do this with servlets (im still a real newbie to serverside java so I was hoping to do it with servlet since it looked simpler than JSP).
I was hoping to get some pointers on or perhaps an algorithm to how this is done with servlets..
My general idea so far is:
1. user makes call to url and it displays webpage with 2 list boxes.
BoxA is initially populated with dates, BoxB is empty.
2. User selects date from BoxA, how would you get your servlet to populate
names in BoxB.
Ive seen examples that use Javascript/onchange() for drop down boxes but they populate the other drop down box with values that are already hard coded into html. Hard coding it into HTML for my project wouldnt be feasible due to the number of items that could be in BoxB. Anyone have any ideas on how to populate BoxB?
Ive already created this project as a java applet at Basically what i want to do is translate this applet into a servlet, due to the fact that 99% of the people use the old MS JVM that wont support this applet...
Again, any ideas, pointers, tips, whatever would be greatly appreciated..
Ranch Hand
Posts: 250
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While avoiding hard-coding the values for the contextual interactivity in the drop-down boxes it would be advisable to generate the client side JavaScript dynamically (using JSP/Servlet).
Once you have all the details on this page you are saved a round-trip to server when user selects something in BoxA. Scripting can help you populate BoxB values from, may be, arrays that holds related data.
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If on the other hand, you don't want to 'pre-load' the entire contents of boxB with all possible values, you are forced into a server round-trip.

I'm not sure if this qualifies as an 'algorithm', but here's a few of my thoughts.

form.jsp contains boxA and boxB.

BoxA (and boxB I suppose) are inside a form.

There is javascript that submits the form when the dropA is changed (onChange is it?)
It submits to a servlet, say "/getBoxB"

Inside getBoxB, you retrieve the current value of boxA, and use it to query your database (or whatever) for the corresponding values of boxB, which you might store into a LinkedHashMap (key would be the value of the boxB, value would be the displayed value of boxB).

This linkedHashMap would then be placed into the request, along with the 'current' value of boxA.

You would also retrieve the values of boxA, although you might want to be clever and do this once, and stick it in the application namespace (ServletContext for servlets, 'application' in a JSP page -> they're the same thing).

The servlet then forwards along (back) to form.jsp

form.jsp will use the 'current' value of the boxA parameter to determine which element of boxA is currently highlighted, when it builds boxA dynamically.

it will iterate over the map to build boxB dynamically.

You wouldn't necessarily need to build boxA dynamically, you *could* use client-side javascript (that was generated dynamically by your JSP) to select the correct value when the page loads.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic