<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'showEmp.jsp' starting page</title> <Meta http-equiv="pragma" content="no-cache"> <Meta http-equiv="cache-control" content="no-cache"> <Meta http-equiv="expires" content="0"> <Meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <Meta http-equiv="description" content="This is my page"> <script type="text/javascript"> var xmlHttp; var x,xx; function showEmps(){ if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); }else if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }else{ alert("暂不支持XMLHttpRequest对象"); return; } if(!xmlHttp){ alert("创建XMLHttpRequest对象失败!"); return; } xmlHttp.onreadystatechange = callback; var url = '/ajax/servlet/EmpQueryServlet'; xmlHttp.open("get",url,true); xmlHttp.send(); } function callback(){ if(xmlHttp.readyState == 4){ if(xmlHttp.status == 200){ var inHtml = "<table border='1px' style='border-collapse: collapse;' width='80%'>"; inHtml += "<tr>"; inHtml += "<th>部门</th>" inHtml += "<th>编号</th>" inHtml += "<th>姓名</th>" inHtml += "<th>工作</th>" inHtml += "<th>上级</th>" inHtml += "<th>就职日期</th>" inHtml += "<th>工资</th>" inHtml += "</tr>"; x = xmlHttp.responseXML.documentElement.getElementsByTagName("person"); //xml文档对象 for(var i=0; i < x.length; i++){ inHtml += "<tr>"; xx = x[i].getElementsByTagName("dname"); inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>"; xx = x[i].getElementsByTagName("empno"); inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>"; xx = x[i].getElementsByTagName("ename"); inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>"; xx = x[i].getElementsByTagName("job"); inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>"; xx = x[i].getElementsByTagName("mgr"); inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>"; xx = x[i].getElementsByTagName("hiredate"); inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>"; xx = x[i].getElementsByTagName("sal"); inHtml += "<td>"+xx[0].firstChild.nodeValue+"</td>"; inHtml += "<tr>"; } inHtml += "</table>"; document.getElementById("showEmp").innerHTML = inHtml; } } } </script> </head> <body> <input type="button" onclick="showEmps()" value="显示所有员工" /> <hr/> <div id="showEmp"></div> <hr/> <table border="1px" style="border-collapse: collapse;" width="80%"> </table> </body> </html>
服务器XML文件:
StringBuilder builder = new StringBuilder(); builder.append("<emp>"); for(int i = 0; i< empList.size(); i++){ Object [] objects = (Object[])empList.get(i); builder.append("<person>"); builder.append("<dname>"+objects[0].toString()+"</dname>"); builder.append("<empno>"+objects[1].toString()+"</empno>"); builder.append("<ename>"+objects[2].toString()+"</ename>"); builder.append("<job>"+objects[3].toString()+"</job>"); builder.append("<mgr>"+objects[4].toString()+"</mgr>"); builder.append("<hiredate>"+objects[5].toString()+"</hiredate>"); builder.append("<sal>"+objects[6].toString()+"</sal>"); builder.append("</person>"); } builder.append("</emp>"); out.println(builder.toString());