转载自:..
[url]http://yangzc106.iteye.com/blog/1030473[/url]
创建类:WebSearchServlet.java
Java代码
package org.search.servlet;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;
public class WebSearchServlet extends WebSocketServlet{
/**
*
*/
private static final long serialVersionUID = 5136484809757218548L;
public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) {
return new SearchServlet();
}
}
创建类:SearchServlet.java
Java代码
package org.search.servlet;
import org.eclipse.jetty.websocket.WebSocket;
public class SearchServlet implements WebSocket{
public void onClose(int arg0, String arg1) {
System.out.println("onClose");
}
public void onOpen(final Connection conn) {
System.out.println("onOpen");
new Thread() {
@Override
public void run() {
try {
conn.sendMessage("sdfsdf");
int i =0;
while (true) {
sleep(1000);
conn.sendMessage("Count:" + (i++));
}
} catch (final Exception e) {
System.err.println(e.getMessage());
}
}
}.start();
}
}
在JSP目录下新建文件comet.html内容如下:
Java代码
<html>
<head>
<title>WebSoket Demo</title>
<script type="text/javascript">
if (!window.WebSocket) {
alert("WebSocket not supported by this browser!");
}
function display() {
var valueLabel = document.getElementById("valueLabel");
valueLabel.innerHTML = "";
var ws = new WebSocket("ws://localhost:9999/comet.do");
ws.onmessage = function(evt) {
valueLabel.innerHTML = evt.data;
};
ws.onclose = function() {
};
ws.onopen = function() {
ws.send("Hello, Server!");
};
}
</script>
</head>
<body οnlοad="display();">
<div id="valueLabel"></div>
</body>
</html>
运行jetty.访问地址http://localhost:9999/comet.html效果如下:
[url]http://yangzc106.iteye.com/blog/1030473[/url]
创建类:WebSearchServlet.java
Java代码
package org.search.servlet;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;
public class WebSearchServlet extends WebSocketServlet{
/**
*
*/
private static final long serialVersionUID = 5136484809757218548L;
public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) {
return new SearchServlet();
}
}
创建类:SearchServlet.java
Java代码
package org.search.servlet;
import org.eclipse.jetty.websocket.WebSocket;
public class SearchServlet implements WebSocket{
public void onClose(int arg0, String arg1) {
System.out.println("onClose");
}
public void onOpen(final Connection conn) {
System.out.println("onOpen");
new Thread() {
@Override
public void run() {
try {
conn.sendMessage("sdfsdf");
int i =0;
while (true) {
sleep(1000);
conn.sendMessage("Count:" + (i++));
}
} catch (final Exception e) {
System.err.println(e.getMessage());
}
}
}.start();
}
}
在JSP目录下新建文件comet.html内容如下:
Java代码
<html>
<head>
<title>WebSoket Demo</title>
<script type="text/javascript">
if (!window.WebSocket) {
alert("WebSocket not supported by this browser!");
}
function display() {
var valueLabel = document.getElementById("valueLabel");
valueLabel.innerHTML = "";
var ws = new WebSocket("ws://localhost:9999/comet.do");
ws.onmessage = function(evt) {
valueLabel.innerHTML = evt.data;
};
ws.onclose = function() {
};
ws.onopen = function() {
ws.send("Hello, Server!");
};
}
</script>
</head>
<body οnlοad="display();">
<div id="valueLabel"></div>
</body>
</html>
运行jetty.访问地址http://localhost:9999/comet.html效果如下: