Click to See Complete Forum and Search --> : Dynamic dropdown box


sjklein
07-11-2006, 02:41 PM
Let me start by saying I'm not sure if this thread belongs here or in the JavaScript forum, so I apologize if I choose the wrong one.

I have a jsp page that reads data from a database and creates a dropdown list from the data. After the user chooses an item from the first dropdown, I need to query the database and get the values to populate the second dropdown.

I'm new to web development, so this is confusing me a little. I need to use Java to get the data from the database (server side). But I need to use JavaScript to change the dropdown list (client side), correct? So how do I make the server side and the client side "talk" to each other?

I was thinking about putting "form.submit" in the first dropdown's onChange and getting the data from the database that way.

Does that make sense? Is that the best way to do it?

Thanks!

Khalid Ali
07-11-2006, 03:33 PM
Yes it makes sense. Because you will have to use some means to send the request to db(you can use the submit form) , and then get the request back from the db and repopulate the next listbox.
You can use XMLHttpRequest object as well for partial submission and data retrieval....

sjklein
07-11-2006, 04:24 PM
Please forgive my ignorance. I've been looking into the XMLHttpRequest object and now I'm really confused. I found how to create the object, open the connection, etc. But the tutorials I looked at only showed the client side. Where do I put the server side code? Maybe I need a better tutorial. Do you have a recommendation?

Again, thank you for the help!

Khalid Ali
07-12-2006, 12:05 AM
No, what you can do is create a servlet, which takes some parameters.
Then process those parameters and send a response back and process the response using JavaScript and populate ur listboxes.
In the xmlhttprequest object send the request to your servlet...

sjklein
07-13-2006, 01:09 PM
Okay, so here's what I will do:

somefile.jsp
java code to get initial database calls
javascript code to call servlet code when dropdown selection changes
html form, etc.

somefile.java
java code to take input from javascript code, run db query, and then pass back data


Is that correct? With regards to a servlet, how does it get compiled? What is the url to a servlet? With my regular jsp files, the url (for now) is localhost:8080/dir/file.jsp. However, when I tried localhost:8080/dir/file.java, Apache told me the file was unavailable. Maybe it's "illegal" to call a servlet directly?

Again, thank you so much for your help. I sort of feel like I'm swimming under water - I can see/understand the basics, but the details are still blury.

Thanks!