jsp實現將資訊放入xml中的方法_JSP編程

來源:互聯網
上載者:User

本文執行個體講述了jsp實現將資訊放入xml中的方法。分享給大家供大家參考,具體如下:

一、jsp代碼:

省: <select id="province" name="province" onchange="jsSubmit()">     <option value="北京" selected="selected">北京</option>     <option value="廣東">廣東</option>     <option value="海南">海南</option>   </select> 市: <select id="city" name="city">     <option value="北京">北京</option>  </select>

二、ajax代碼建立伺服器請求代碼不用寫了,寫onchange時候的事件jsSubmit吧:

function jsSubmit() {   createXMLHttpRequest();      var province = document.getElementById("province");   //解決用戶端向伺服器端傳輸中文亂碼      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;//回呼函數啊!   xmlHttp.send(null); }

三、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 = { "海口", "瓊海", "三亞" };   String[] cities2 = { "廣州", "珠海", "佛山", "深圳" };   String[] cities3 = { "北京" };   map.put("北京", cities3);   map.put("廣東", cities2);   map.put("海南", cities1);  } st方法{   String province = request.getParameter("value");// 解決用戶端向伺服器端傳輸中文亂碼   String proviceCN = URLDecoder.decode(province, "UTF-8");   String[] cities = map.get(proviceCN);//根據傳來的省,查出已經存放進map中對應的市   response.setContentType("text/xml; charset=UTF-8");   StringBuffer buff=new StringBuffer("<citylist>");///準備拼字串......    for (String city : cities)     {      buff.append("<city>").append(city) .append("</city>");     }   buff.append("</citylist>");   response.getWriter().println(buff.toString());

四、ajax的回呼函數

function processResponse() {   if(xmlHttp.readyState == 4) {    if(xmlHttp.status == 200) {     var cities = xmlHttp.responseXML.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;//用firstChild方法,其他方法我用text方法不管用~不知道怎麼回事       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("請求的頁面有異常");    }  } }

希望本文所述對大家jsp程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.