JSP implements the method of putting information into XML _jsp programming

Source: Internet
Author: User
Tags stringbuffer

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.