File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes updating drop down list boxes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "updating drop down list boxes" Watch "updating drop down list boxes" New topic
Author

updating drop down list boxes

Chad Patrick
Greenhorn

Joined: Feb 01, 2003
Posts: 11

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 http://24.236.86.244/p20/ 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..
Thanks
Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 225

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.


Debashish
SCJP2, SCWCD 1.4
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: updating drop down list boxes
 
Similar Threads
Ajax: how to achieve
dependent select boxes with ajax .
search from list.
2 Select Box`s on one page, second needs to load dynamically
Adding UI controlls dynamically