Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drop Down Menu Using AJAX and JSP

 
Robert Merrett
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Pie
    Posts: 65105
    89
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic