This article illustrates how JSP implementations put information into XML. Share to everyone for your reference, specific as follows:
One, JSP code:
Province: <select id= "province" Name= "province" onchange= "Jssubmit" () "> <option value=" Beijing "selected="
> Beijing </option>
<option value= "Guangdong" > Guangdong </option>
<option value= "Hainan" > Hainan </option >
</select>
: <select id= "City" name= "Cities" >
<option value= "Beijing" > Beijing </option >
</select>
Second, the AJAX code to create the server request code do not write, write onchange when the event Jssubmit bar:
function Jssubmit () {
createxmlhttprequest ();
var province = document.getElementById ("province");
Resolves the client to the server to transmit Chinese garbled
var uri = "ajaxaction?value=" + encodeURI (encodeURI (Province.value));
Xmlhttp.open ("POST", Uri, true);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded;")
Xmlhttp.onreadystatechange = processresponse;//callback function Ah!
xmlhttp.send (null);
}
Third, the servlet
public class Ajaxaction extends HttpServlet {private static final long Serialversionu
ID = 1L;
private static map<string, string[]> Map = new hashmap<string, string[]> ();
static {string[] Cities1 = {"Haikou", "Qionghai", "Sanya"};
String[] Cities2 = {"Guangzhou", "Zhuhai", "Foshan", "Shenzhen"};
String[] Cities3 = {"Beijing"};
Map.put ("Beijing", Cities3);
Map.put ("Guangdong", Cities2);
Map.put ("Hainan", cities1); } St Method {string province = Request.getparameter ("value");//Resolve client transmits Chinese garbled String PROVICECN = Urldecoder.decode to Server (pr
Ovince, "UTF-8"); string[] Cities = map.get (PROVICECN); According to the province, find the corresponding city Response.setcontenttype ("Text/xml") which has been deposited into the map;
Charset=utf-8 "); StringBuffer buff=new stringbuffer ("<citylist>");///prepare the spelling string ... for (string city:cities) {Buff.ap
Pend ("<city>"). Append (city). Append ("</city>");
} buff.append ("</citylist>");
Response.getwriter (). println (Buff.tostring ());
Four, the Ajax callback function
function ProcessResponse () {
if (xmlhttp.readystate = 4) {
if (Xmlhttp.status =) {
var cities = XMLHTTP.R Esponsexml.getelementsbytagname ("City");
var displayselect = document.getElementById ("city");
displayselect.innerhtml = null;
for (var i= 0; i < cities.length; i++) {
if (i = = 0) {
var a= xmlHttp.responseXML.getElementsByTagName ("City") [ i].firstchild.data;//use FirstChild method, other methods I use the text method no matter ~ I don't know what's going on
var op = new Option (A, A, true, true);
} else {
var a= xmlHttp.responseXML.getElementsByTagName ("City") [I].firstchild.data;
var op = new Option (A, a);
alert (a);
}
Displayselect.options[i] = op;
}
else {
Window.alert ("The requested page has an exception");}}
I hope this article will help you with JSP program design.