#2
hwj200701182007-04-30 09:42
|
三个部分组成:
1,我的JSP代码:
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<!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=gb2312">
<title>无标题文档</title>
<script language="javascript">
var http_request=false;
function createXMLHttpRequest()
{
if(window.XMLHttpRequest)
{
http_request=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try{
http_request=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e)
{
try{
http_request=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
}
function sendRequest()
{
createXMLHttpRequest();
var url="refresh";
http_request.open("GET",url,true);
http_request.onreadystatechange=processRequest;
http_request.send(null);
}
function processRequest()
{
if(http_request.readystate==4)
{
if(http_request.status==200)
{
display();
setTimeout("display()",1000);
}
else
{alert("出错了!");}
}
}
function display()
{
var name=http_request.responseXML.getElementByTagName("name")[0].firstChild.nodeValue;
var num=http_request.responseXML.getElementByTagName("num")[0].firstChild.nodeValue;
document.getElementById("product").innerHTML=name;
document.getElementById("count").innerHTML=num;
}
</script>
</head>
<body onload="sendRequest()">
<table width="257" height="81" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan=2><div align="center">正在热卖的笔记本</div></td>
</tr>
<tr>
<td>型号</td>
<td id="product"></td>
</tr>
<tr>
<td>销售数量</td>
<td id="count"></td>
</tr>
</table>
</body>
</html>
2,我的servlet代码:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class servlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
resp.setContentType("text/xml;charset=UTF-8");
resp.setHeader("Cache-Control","no-cache");
PrintWriter out=resp.getWriter();
out.println("<response>");
out.println("<name>IBM</name>");
out.println("<num>160</num>");
out.println("</response>");
}
}
3,我的web.xml配置:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>a</servlet-name>
<servlet-class>servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/refresh</url-pattern>
</servlet-mapping>
</web-app>
请大家,看一下,哪里出错了.谢谢.