<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>小组管理系统</title>
<link rel="stylesheet" type="text/css" href="Css/c.css">
<script type="text/javascript" language="javascript">
var xmlHttp;
var req_url;
function createXMLHttpRequest(){
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else{
if(window.ActiveXObject){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHttp");
}catch(e){
}
}
}
}
}
function startRequest(url){
req_url=url;
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET",req_url,true);
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
if(req_url=="servlet/ViewTeamsXml"){
var dtable=document.getElementById("dataBody");
//刷新前清空以前的结果
var length=dtable.rows.length;
for(var i=0;i<length;i++){
dtable.deleteRow(0);
}
var teamEls=xmlHttp.responseXML.getElementsByTagName("team");
for(var i=0;i<teamEls.length;i++){
var ElTr=dtable.insertRow(i);
var teamEl=teamEls.item(i);
var idEl=teamEl.getElementsByTagName("id");
var
id=idEl.item(0).firstChild.nodeValue;
var elTdId=ElTr.insertCell(0);
elTdId.innerHTML=id;
var nameEl=teamEl.getElementsByTagName("name");
var name=" ";
if(nameEl.item(0).firstChild!=null){
name=nameEl.item(0).firstChild.nodeValue;
}
var elTdName=ElTr.insertCell(1);
elTdName.innerHTML=name;
var sloganEl=teamEl.getElementsByTagName("slogan");
var slogan=" ";
if(sloganEl.item(0).firstChild!=null){
slogan=sloganEl.item(0).firstChild.nodeValue;
}
var elTdSlogan=ElTr.insertCell(2);
elTdSlogan.innerHTML=slogan;
var leaderEl=teamEl.getElementsByTagName("leader");
var leader=" ";
if(leaderEl.item(0).firstChild!=null){
leader=leaderEl.item(0).firstChild.nodeValue;
}
var elTdLeader=ElTr.insertCell(3);
elTdLeader.innerHTML=leader;
}
}
}
}
}
</script>
</head>
<body>
<div id="navigate">
<h1>小组管理系统</h1>
<p><a href="javascript:startRequest('servlet/ViewTeamsXml')">显示小组</a></p>
</div>
<div id="show">
<table border="1">
<tr>
<th>ID</th><th>组名</th><th>口号</th><th>组长</th>
</tr>
<tbody id="dataBody">
</tbody>
</table>
</div>
</body>
</html>
ViewTeamsXml.java
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import business.TeamBusiness;
import org.dom4j.*;
public class ViewTeamsXml extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
response.setContentType("text/xml;charset=UTF-8");
PrintWriter out=response.getWriter();
TeamBusiness teamBusiness=new TeamBusiness();
Document document =teamBusiness.allTeamsXml();
//out.println("<?xml-stylesheet type=\"text/xsl\" href=\"../xsl/teaminfo.xsl\"?>");
out.println(document.getRootElement().asXML());
}
}
allTeamsXML方法:
public Document allTeamsXml() {
Document document = DocumentHelper.createDocument();
Element rootElm = document.addElement("teaminfo");
DataSource ds = new DataSource();
Connection cn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
cn = ds.getConnection();
String sql = "select id,name,slogan,leader from team";
pst = cn.prepareStatement(sql);
rs = pst.executeQuery();
Element teamsElm = rootElm.addElement("teams");
while(rs.next()) {
Element teamElm = teamsElm.addElement("team");
Element idElm = teamElm.addElement("id");
idElm.setText(rs.getString("id"));
Element nameElm = teamElm.addElement("name");
nameElm.setText(rs.getString("name"));
Element sloganElm = teamElm.addElement("slogan");
sloganElm.setText(rs.getString("slogan"));
Element leaderElm = teamElm.addElement("leader");
leaderElm.setText(rs.getString("leader"));
}
}
catch(Exception e) {
e.printStackTrace();
Element errorElm = rootElm.addElement("error");
errorElm.setText(e.getMessage());
}
finally {
try {
rs.close();
pst.close();
cn.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
return document;
}