SoFunction
Updated on 2025-03-10

jsp implements the method of putting information into xml

This article describes the method of putting information into XML by JSP. Share it for your reference, as follows:

1. jsp code:

Province: <select  name="province" onchange="jsSubmit()"> 
    <option value="Beijing" selected="selected">Beijing</option> 
    <option value="Guangdong">Guangdong</option> 
    <option value="Hainan">Hainan</option> 
  </select> 
city: <select  name="city"> 
    <option value="Beijing">Beijing</option> 
 </select>

2. You don’t need to write the ajax code to create server request code. You can write the event jsSubmit when writing onchange:

function jsSubmit() { 
  createXMLHttpRequest(); 
     var province = ("province"); 
  //Solve that the client transmits Chinese garbled code to the server side     var uri = "AjaxAction?value=" + encodeURI(encodeURI()); 
  ("POST", uri, true); 
  ("Content-Type","application/x-www-form-urlencoded;") 
   = processResponse;//Callback function!  (null); 
}

3. servlet

public class AjaxAction extends HttpServlet { 
 private static final long serialVersionUID = 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" }; 
  ("Beijing", cities3); 
  ("Guangdong", cities2); 
  ("Hainan", cities1); 
 } 
stmethod{ 
  String province = ("value");// Solve the client to transmit Chinese garbled code to the server  String proviceCN = (province, "UTF-8"); 
  String[] cities = (proviceCN);// According to the province that was sent, find out the corresponding city that has been stored in the map  ("text/xml; charset=UTF-8"); 
  StringBuffer buff=new StringBuffer("<citylist>");///Prepare the spelling string...   for (String city : cities) 
    { 
     ("<city>").append(city) .append("</city>"); 
    } 
  ("</citylist>"); 
  ().println(());

4. Ajax callback function

function processResponse() { 
  if( == 4) { 
   if( == 200) { 
    var cities = ("city"); 
    var displaySelect = ("city"); 
     = null; 
    for (var i= 0 ;i <  ; i++){    
     if (i == 0) {       
     var a= ("city")[i].;//Use the firstChild method, other methods, I use the text method and it doesn't work~ I don't know what's going on      var op = new Option(a, a, true, true); 
     } else { 
      var a= ("city")[i].; 
      var op = new Option(a, a); 
      alert(a); 
     } 
     [i] = op; 
    }     
   } else { 
    ("The requested page has an exception"); 
   } 
 } 
}

I hope this article will be helpful to everyone's jsp programming.