Sie sind auf Seite 1von 2

Use AJAX.

Write an onSelect javascript handler for the first combobox, whose implementation
gets the XMLHttpRequest and sends this request to your (special) servlet that should be able to
build JUST the HTML of the second combobox. The result HTML is accessible in your javascript
code so that you can set it as the HTML content of the DIV containing the second combobox.
Please note that you can even do this asynchronously, by marking the XMLHttpRequest as
asynchronous and registering a javascript 

i have to bind two combo boxes dynamically .Example i have to get list of states based on country
selected.All this code i have to write in jsp servlets and ajax can any one pls give me code for this
ou can use XMLHttpRequest (the essence of AJAX) to send a message to the server and it will
return some data with or without a browser refresh. Then, you can use DOM (Document Object
Model) to update the page. Normally, AJAX does a page refresh, but that can be bypassed by 'url
tagging.' Note that this method is very unsecured. In short, XMLHttpRequest accomplishes the
same thing is form submission; the server side script still receives an HTTP request and responds
accordingly.

/////////////////////////<countries.js…
<%
String country = request.getParameter("country_name");
//search your database to get states 
String[] states = getStates(country);
response.println(Arrays.toString(states)… 
%>
/////////////////////////
<html>
<script>

//This AJAX call will get a list of states 

function getStates() {

//get country name


var country = document.getElementById("country");

//define the sending parameters; here we will use the HTTP method GET, so that that data is
//passed through URL variables. 

url = "http://localhost:<port #>/countries.jsp?country_name='+country+…


xmlhttp.open("GET", url, true);

//define AJAX listener and how to update the page

xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {

//get server data into an array of strings; responseText will return a string, so tokenize it in JScript

var array = tokenize(xmlhttp.responseText);


var divisor = document.getElementById('myDiv')

//use DOM to update HTML table

for(i = 0; i < array.length; i++) {


var txt = document.createTextNode(array[i]);
divisor.appendChild(txt); 
}
}
}//end listener
xmlhttp.setRequestHeader('Accept','messa…
//send request
xmlhttp.send(); 

}//end function
</script>

Country: <input type = "text" value = "" name = "country"/><br/>


<input type = "submit" value = "Get States" onsubmit = "getStates()"/><br/>
States<br/>
<div name = "myDiv">
</div>
</html>

Obviously, you wouldn't want the user to enter the name of the country. I just did a text box for
simplicity. If your list of countries changes you could use an AJAX call to present that list as well. I
haven't ran this code, but it's mostly all you need.

http://www.java2s.com/Code/JavaScript/Form-
Control/Disableandenableadropdownlistcombobox.htm
http://www.java2s.com/Code/JavaScript/Form-
Control/ChangingSelectElementContenttwoCombobox.htm

http://www.ibm.com/developerworks/web/library/wa-ajaxintro2/?ca=dgr-
lnxw07AJAX-Request

http://www.roseindia.net/ajax/ajax-books.shtml

http://www.roseindia.net/jsp/comboSelect.shtml

Das könnte Ihnen auch gefallen