wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes Drop Down Menu Using AJAX and JSP 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 "Drop Down Menu Using AJAX and JSP" Watch "Drop Down Menu Using AJAX and JSP" New topic
Author

Drop Down Menu Using AJAX and JSP

Robert Merrett
Greenhorn

Joined: Dec 06, 2011
Posts: 1
Hi All
New to this site, nice stuff you have going here,
I would like to ask for some help with JSP and AJAX, I'm new to that too ;-)
I have a JSP web form with combo boxes,
the combo boxes should be populated with the names from a database table,
I'm cool with connecting to the Database and getting the names, not a problem,
but the real problem I've got is the AJAX to load the names in there and in what form to return the names gathered from the query. I understand there will need to be some form of HTP request and receive statement. but beyond that.. I'm a touch lost
Also the examples that I have seen all seem to have the database connection on the client end... but to my understanding isn't that poor practice?
could someone help me out please? I would really appreciate some very small example that I can sift through and code out myself to see the flow etc!


thanks guys
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

The technology you seek is indeed Ajax, and it's a JavaScript thing.

By the time your page gets to the browser, it's just an HTML page with JavaScript so I'm moving this to the HTML/JavaScript forum as it's not really a JSP question.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

First thing: HtmlHasNoComboBox -- correct terminology is important!

Now, what you need to do is:
  • Establish a change handler on the first dropdown to detect when it is changed.
  • In the handler, grab the selected value and issue an Ajax request to fetch the set of options for the 2nd dropdown.
  • In the Ajax success handler, take the response (the HTML fragment of options), and tack it onto the 2nd dropdown.

  • Ajax is much much easier if you use a JavaScript library such as jQuery, which turns the above into a few lines of code. (In raw JavaScript it's like about 3 dozen or so.)
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60780
        
      65

    One more thing...

    Robert Merrett wrote:Also the examples that I have seen all seem to have the database connection on the client end... but to my understanding isn't that poor practice?

    If by this you mean that the examples had the DB code in the JSP, that's not client-side as the JSP is evaluated on the server in order to create the HTML output that will be sent to the browser.

    But regardless of that, yes, it's a horribly poor practice to put Java code -- especially DB code -- into a JSP.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Drop Down Menu Using AJAX and JSP
     
    Similar Threads
    Select multibox on combo box selection?
    How to connect multiple combo boxes?
    Populating the Combo Box using AJAX
    jsp...urgent help required
    getting values in combobox from database without refreshing