2.前段頁面代碼
<body id="theBody"> <select id="name"> <option value="zhangsan">zhangsan</option> <option value="lisi">lisi</option> </select> <input type="button" id="button1" value="get content from server"> </body>
3.javaBean--Person
public class Person {private int id;private String name;private int age;private String address;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}
4.伺服器端代碼--XMLServlet
protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String name = req.getParameter("name");Person person = new Person();if("zhangsan".equals(name)){person.setId(1);person.setName("zhangsan");person.setAddress("tianjin");person.setAge(23);}else{person.setId(2);person.setName("lisi");person.setAddress("beijing");person.setAge(30);}//產生XML--DOM4JDocument document = DocumentHelper.createDocument();Element rootElement = document.addElement("users");rootElement.addComment("This is a commont");Element userElement = rootElement.addElement("user");Element idElement = userElement.addElement("id");Element nameElement = userElement.addElement("name");Element ageElement = userElement.addElement("age");Element addressElement = userElement.addElement("address");idElement.setText(person.getId()+"");nameElement.setText(person.getName());ageElement.setText(person.getAge()+"");addressElement.setText(person.getAddress());resp.setContentType("text/html; charset=utf-8"); //設定回應標頭resp.setHeader("pragma", "no-cache"); //無緩衝resp.setHeader("cache-control", "no-cache"); //無緩衝PrintWriter out = resp.getWriter();OutputFormat format = OutputFormat.createPrettyPrint(); //為了方便在firebug裡面查format.setEncoding("utf-8");XMLWriter xmlWriter = new XMLWriter(out,format); //寫到用戶端xmlWriter.write(document);out.flush();}
5.前端頁面處理指令碼
$("#button1").click(function(){$.ajax({ //no need to judge the browser typetype: "POST", //類型url: "XMLServlet", //請求訪問的servletdataType: "xml", data: {'name': $("#name").val()}, //傳遞的參數,擷取選擇的值success: function(returnedData){ //成功返回的回呼函數//解析傳來的XML--jQuery對象var id = $(returnedData).find("id").text(); //找到id元素並取其值var name = $(returnedData).find("name").text();var age = $(returnedData).find("age").text();var address = $(returnedData).find("address").text();//建立表格顯示格式var html="<table width='60%' border='1' align='center'><tr><th>id</th><th>name></th><th>age</th><th>address</th></tr><tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+address+"</td></tr></table>";$("#theBody table:eq(0)").remove(); //刪除先前的表格$("#theBody").append(html);}});});
6.前端頁面處理指令碼--簡易形式
$("#button1").click(function(){$.post("XMLServlet",{name:$("#name").val()},function(returnedData,status){//解析傳來的XML--jQuery對象var id = $(returnedData).find("id").text(); //找到id元素並取其值var name = $(returnedData).find("name").text();var age = $(returnedData).find("age").text();var address = $(returnedData).find("address").text();//建立表格顯示格式var html="<table width='60%' border='1' align='center'><tr><th>id</th><th>name></th><th>age</th><th>address</th></tr><tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+address+"</td></tr></table>";$("#theBody table:eq(0)").remove(); //刪除先前的表格$("#theBody").append(html);});});
$("#button1").click(function(){$.post("XMLServlet",{name:$("#name").val()},function(returnedData,status){//解析傳來的XML--jQuery對象var id = $(returnedData).find("id").text(); //找到id元素並取其值var name = $(returnedData).find("name").text();var age = $(returnedData).find("age").text();var address = $(returnedData).find("address").text();//建立表格顯示格式var html="<table width='60%' border='1' align='center'><tr><th>id</th><th>name></th><th>age</th><th>address</th></tr><tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+address+"</td></tr></table>";$("#theBody table:eq(0)").remove(); //刪除先前的表格$("#theBody").append(html);});});
$("#button1").click(function(){$.ajax({ //no need to judge the browser typetype: "POST", //類型url: "XMLServlet", //請求訪問的servletdataType: "xml", data: {'name': $("#name").val()}, //傳遞的參數,擷取選擇的值success: function(returnedData){ //成功返回的回呼函數//解析傳來的XML--jQuery對象var id = $(returnedData).find("id").text(); //找到id元素並取其值var name = $(returnedData).find("name").text();var age = $(returnedData).find("age").text();var address = $(returnedData).find("address").text();//建立表格顯示格式var html="<table width='60%' border='1' align='center'><tr><th>id</th><th>name></th><th>age</th><th>address</th></tr><tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+address+"</td></tr></table>";$("#theBody table:eq(0)").remove(); //刪除先前的表格$("#theBody").append(html);}});});