Jetty+Dojo+Tomcat的Comet学习笔记-HelloWorld
<br />编程环境及配置:<br />Eclipse3.3 + tomcat 6.0 + dojo + jetty 6.1.9<br />主要讲讲jetty。<br />“Jetty是一个开源的servlet容器,它为基于Java的web内容,例如JSP和servlet提供运行环境。Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布。开发人员可以将Jetty容器实例化成一个对
·
编程环境及配置:
Eclipse3.3 + tomcat 6.0 + dojo + jetty 6.1.9
主要讲讲jetty。
“Jetty 是一个开源的servlet容器,它为基于Java的web内容,例如JSP和servlet提供运行环境。Jetty是使用Java语言编写的,它的 API以一组JAR包的形式发布。开发人员可以将Jetty容器实例化成一个对象,可以迅速为一些独立运行(stand-alone)的Java应用提供 网络和web连接。”
Jetty里面含有实现comet技术的包。通过在Eclipse+tomcat编程环境中添加扩展jar文件的方式即可快速实现上述编程环境的配置。
- 首先下载jetty-6.1.9.zip(可到)
- 在他的/lib文件夹下面有我们所需要的所有的jar文件,找到jetty-util-6.1.9.jar,jetty- 6.1.9.jar,servlet-api-2.5-6.1.9.jar以及在/lib/cometd下面的cometd-bayeux- 6.1.9.jar,cometd-api-0.9.20080221.jar,将他们一并导入到eclipse的工程当中。
- 将jetty-6.1.9/contrib/cometd/bayeux/src/main/java下面的org文件夹拷贝至工程的src目录中
HelloWorld
- 编写HelloWorld.java
- package cn.ict.net.lx.comet;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import dojox.cometd.Bayeux;
- import dojox.cometd.Channel;
- /**
- * Servlet implementation class for Servlet: HelloWorld
- *
- */
- public class HelloWorld extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
- static final long serialVersionUID = 1L;
- /* (non-Java-doc)
- * @see javax.servlet.http.HttpServlet#HttpServlet()
- */
- public HelloWorld() {
- super ();
- }
- /* (non-Java-doc)
- * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- //response.getWriter().println("Hello");
- for ( int i= 0 ; i < 100 ; i++)
- publish();
- }
- /* (non-Java-doc)
- * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- publish();
- }
- public void publish(){
- Bayeux b = (Bayeux)getServletConfig().getServletContext().getAttribute(Bayeux.DOJOX_COMETD_BAYEUX);
- Channel c = b.getChannel("/hello/world" , false );
- Map<String, Object> message = new HashMap<String, Object>();
- message.put("test" , "jsp: hello world" );
- c.publish(b.newClient("server_user" , null ), message, "new server message" );
- }
- }
- 配置web工程的web.xml如下
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < web-app id = "WebApp_ID" version = "2.4"
- 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
- < display-name > dojoSrc </ display-name >
- < welcome-file-list >
- < welcome-file > index.html </ welcome-file >
- < welcome-file > index.htm </ welcome-file >
- < welcome-file > index.jsp </ welcome-file >
- < welcome-file > default.html </ welcome-file >
- < welcome-file > default.htm </ welcome-file >
- < welcome-file > default.jsp </ welcome-file >
- </ welcome-file-list >
- < servlet >
- < servlet-name > hello </ servlet-name >
- < servlet-class >
- cn.ict.net.lx.comet.HelloWorld
- </ servlet-class >
- </ servlet >
- < servlet-mapping >
- < servlet-name > hello </ servlet-name >
- < url-pattern > /hello/* </ url-pattern >
- </ servlet-mapping >
- <!-- 配置ContinuationCometdServlet, 这个是必须的。配置后就可以支持comted -->
- < servlet >
- < servlet-name > cometd </ servlet-name >
- < servlet-class >
- org.mortbay.cometd.continuation.ContinuationCometdServlet
- </ servlet-class >
- < load-on-startup > 1 </ load-on-startup >
- </ servlet >
- <!-- HelloWorld的servlet配置 -->
- < servlet >
- < description >
- </ description >
- < display-name >
- HelloWorld</ display-name >
- < servlet-name > HelloWorld </ servlet-name >
- < servlet-class >
- cn.ict.net.lx.comet.HelloWorld</ servlet-class >
- </ servlet >
- <!-- Servlet映射 -->
- < servlet-mapping >
- < servlet-name > cometd </ servlet-name >
- < url-pattern > /cometd/* </ url-pattern >
- </ servlet-mapping >
- < servlet-mapping >
- < servlet-name > HelloWorld </ servlet-name >
- < url-pattern > /HelloWorld </ url-pattern >
- </ servlet-mapping >
- </ web-app >
- 编写hello.html
- <!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 > Comet Hello World </ title >
- < mce:script type = "text/javascript" src = "../dojo/dojo/dojo.js" mce_src = "dojo/dojo/dojo.js" djConfig = "parseOnLoad:true" > </ mce:script >
- < mce:script type = "text/javascript" > <!--
- dojo.require("dojox.cometd");
- dojo.require("dojox.cometd.timestamp");
- function callback(msg) {
- // alert(msg);
- // document.write(msg);
- // alert(msg.data.test);
- document.body.appendChild(document.createTextNode(msg.data.test+"< br > "));
- }
- dojox.cometd.init("/dojoSrc/cometd/cometd");
- dojox.cometd.subscribe("/hello/world", callback);
- // --> </ mce:script >
- </ head >
- < body >
- < input type = "button"
- onclick = "dojox.cometd.publish('/hello/world', { test: 'hello world' } )"
- value = "Click Me!" >
- </ body >
- </ html >
分别使用IE/Firefox/Chrome访问页面hello.html:
更多推荐
已为社区贡献1条内容
所有评论(0)