json servlet通訊 顯示資料

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

servlet

 1 //輸出JSON格式的省份資訊 2 @WebServlet("/ServletDemo1") 3 public class ServletDemo1 extends HttpServlet { 4     private static final long serialVersionUID = 1L; 5         6     7     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 8         response.setContentType("text/html;charset=UTF-8"); 9         PrintWriter out = response.getWriter();10         String str = "{name:‘山東省‘}";11         out.write(str);12     }

json   jsp中應用

eval函數用於轉換成 json的文本 來讓以後代碼使用
 1 <%@ page language="java"  pageEncoding="utf-8"%> 2  3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5     <head> 6         <title>串聯功能表</title> 7         <script type="text/javascript" src="./xmFile.js"> </script> 8     </head> 9     <body>10      <select id="province" name="province">11        <option value="">請選擇....</option>12      </select>13      <select id="city" name="city">14          <option value="">請選擇.....</option>15      </select>16      <script type="text/javascript">17          window.onload=function(){18              var xhr = createXmlHttpRequest();19              xhr.onreadystatechange=function(){20                  if(xhr.readyState==4){21                      if(xhr.status==200||xhr.status==304){22                          var data = xhr.responseText;//JSON資料,服務端是一般字元串返回的23                          var provinceJson = eval("("+data+")");//把普通的JSON字串文本變成真正的JSON資料24                          25                          var optionElement = document.createElement("option");26                          optionElement.setAttribute("value",provinceJson.name);27                          var textNode = document.createTextNode(provinceJson.name);28                          optionElement.appendChild(textNode);29                          30                          document.getElementById("province").appendChild(optionElement);31                          32                      }33                  }34              }35              xhr.open("GET","${pageContext.request.contextPath}/ServletDemo1?time="+new Date().getTime());36              xhr.send(null);37          }38          function createXmlHttpRequest(){39                var xmlHttp;40                try{    //Firefox, Opera 8.0+, Safari41                        xmlHttp=new XMLHttpRequest();42                 }catch (e){43                        try{    //Internet Explorer44                               xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");45                         }catch (e){46                               try{47                                       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");48                               }catch (e){}  49                        }50                 }51                return xmlHttp;52              }53      </script>54   </body>55 </html>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.