我有一个使用CometProcessor接口的简单servlet。
package cc.co.sqeezer;
import java.io.IOException;
public class TestServlet extends HttpServlet implements CometProcessor {
private static final long serialVersionUID = 1L;
public TestServlet() {
super();
}
public void event(CometEvent event) {
if (event.getEventType() == CometEvent.EventType.BEGIN)
{
System.out.println("Begin");
event.getHttpServletRequest().setAttribute("org.apache.tomcat.comet.timeout", new Integer(0xFFFFFFFF));
send(event);
}
else if (event.getEventType() == CometEvent.EventType.READ)
{
System.out.println("Read");
send(event);
}
else if (event.getEventType() == CometEvent.EventType.END)
{
System.out.println("End: " + event.getEventSubType());
send(event);
}
else if (event.getEventType() == CometEvent.EventType.ERROR)
{
System.out.println("Error: " + event.getEventSubType());
send(event);
}
}
private void send(CometEvent event) {
HttpServletResponse response = event.getHttpServletResponse();
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "must-revalidate");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
String eventType = event.getEventType().toString();
String receivedText = (String) event.getHttpServletRequest().getParameter("mytext");
try {
response.getWriter().write(eventType + " " + receivedText);
response.getWriter().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void init(ServletConfig config) throws ServletException {
}
}
连接器
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
URIEncoding="UTF-8"
redirectPort="8443" />
Jsp页面是
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Test</title>
</head>
<body>
<script type="text/javascript"><!--
function createRequestObject() {
if (typeof XMLHttpRequest === 'undefined') {
XMLHttpRequest = function() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); }
catch(e) {}
throw new Error("This browser does not support XMLHttpRequest.");
};
}
return new XMLHttpRequest();
}
var req = createRequestObject();
function sendData()
{
if (req) {
req.open("POST", "TestServlet", true);
req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var params = "mytext=" + document.getElementById("mytext").value;
//req.setRequestHeader("Content-length", params.length);
req.onreadystatechange = processReqChange;
req.send(params);
}
}
function processReqChange()
{
try
{
if (req.readyState == 4) {
alert(req.status + " ReadyState=4 " + req.responseText);
}
if (req.readyState == 3) {
alert(req.status + " ReadyState=3 " + req.responseText);
}
if (req.readyState == 2) {
alert(req.status + " ReadyState=2 " + req.responseText);
}
if (req.readyState == 1) {
alert(req.status + " ReadyState=1 " + req.responseText);
}
}
catch( e ) {
//alert("Error: " + e.description);
}
}
--></script>
<input id="mytext" type="text"></input>
<input type="button" value="send" onclick="sendData()"></input>
</body>
</html>
我正在使用Eclipse来启动servlet
我期望的是将“文本”发送到服务器并使用相同的文本和CometEvent名称返回echo。
当我将数据发送到服务器时,我首先得到“200 ReadyState = 3 BEGIN text”。这是预期的情况。如果我等待大约25秒,我将在控制台输出中得到“错误:超时”。并且错误事件将每25秒触发一次,因此在浏览器中我看到类似“200 ReadyState = 3 BEGIN textERROR textERROR textERROR textERROR textERROR text”,其中“text”是输入中的文本。 5 textERROR表示发生了5个错误事件。我不知道为什么这些textERROR出现了。主要问题是为什么错误事件(超时)每25秒触发一次?
如果我在浏览器中关闭客户端,我将获得无限循环的结束事件。为什么会发生这种情况我也不知道?如何防止这种循环?
Tomcat版本是Ubuntu 10.10上的Apache Tomcat / 6.0.28
提前谢谢你。
答案 0 :(得分:2)
首先,您可以像这样设置请求的超时,
if (event.getEventType() == CometEvent.EventType.BEGIN) {
event.setTimeout(60 * 1000);
...
}
...
25秒可能是默认值。
发生超时时发送错误,因此每25秒
response.getWriter().write(eventType + " " + receivedText);
在你的代码中发送“错误文本”,而不是textERROR,在你的响应结尾添加一个新行来验证这一点。
无限循环的原因是你没有关闭事件。调用CometEvent ERROR或END时,必须调用event.close(),
else if (event.getEventType() == CometEvent.EventType.END){
event.close();
....
这很可能是你无限循环的原因。
请阅读documentation,它解释了您遇到的大多数问题。