问题描述
在网上查找了一些资料后尝试着自己写了下comet的东西,因为对tomcat比较熟悉,而且据说tomcat6对comet是支持的,所以就基于tomcat6按照网上查找的资料写了点,这里我用的是tomcat6.0.26;大概的意思就是现在有一个实现了org.apache.catalina.CometProcessor接口的Sevlets,不是使用平常的服务方法,就是处理请求不是通过doGet()或doPost(),而是通过CometProcessor的event(),这里就出现了一个问题,当我在js中发出请求后,用firebug进行调试报出的错误是“405MethodNotAllowed”,而如果还是用doGet()或doPost()来处理请求又是正常的了servlet的代码:publicclassWeatherServletextendsHttpServletimplementsCometProcessor{privatestaticfinallongserialVersionUID=1L;privateMessageSendermessageSender=null;privatestaticfinalIntegerTIMEOUT=60*1000;@Overridepublicvoiddestroy(){messageSender.stop();messageSender=null;}@Overridepublicvoidinit()throwsServletException{messageSender=newMessageSender();ThreadmessageSenderThread=newThread(messageSender,"MessageSender["+getServletContext().getContextPath()+"]");messageSenderThread.setDaemon(true);messageSenderThread.start();}@Overridepublicvoidevent(finalCometEventevent)throwsIOException,ServletException{System.out.println("Comet!!!!!");HttpServletRequestrequest=event.getHttpServletRequest();HttpServletResponseresponse=event.getHttpServletResponse();if(event.getEventType()==CometEvent.EventType.BEGIN){request.setAttribute("org.apache.tomcat.comet.timeout",TIMEOUT);log("Beginforsession:"+request.getSession(true).getId());//使用一个MessageSender类发送数据messageSender.setConnection(response);Weathermanweatherman=newWeatherman();weatherman.start();}elseif(event.getEventType()==CometEvent.EventType.ERROR){log("Errorforsession:"+request.getSession(true).getId());event.close();}elseif(event.getEventType()==CometEvent.EventType.END){log("Endforsession:"+request.getSession(true).getId());event.close();}elseif(event.getEventType()==CometEvent.EventType.READ){thrownewUnsupportedOperationException("Thisservletdoesnotacceptdata");}}privateclassWeatherman{publicvoidstart(){Runnabler=newRunnable(){@SuppressWarnings("unchecked")publicvoidrun(){while(true){try{//这里做些事情Thread.sleep(30000L);}catch(Exceptione){//justeatit,eatitSystem.out.println("justeatit,eatit");}}}};Threadt=newThread(r);t.start();}}privateclassMessageSenderimplementsRunnable{protectedbooleanrunning=true;protectedfinalArrayList<String>messages=newArrayList<String>();privateServletResponseconnection;privatesynchronizedvoidsetConnection(ServletResponseconnection){this.connection=connection;notify();}publicvoidsend(Stringmessage){synchronized(messages){messages.add(message);log("Messageadded#messages="+messages.size());messages.notify();}}publicvoidstop(){running=false;}publicvoidrun(){while(running){if(messages.size()==0){try{synchronized(messages){messages.wait();}}catch(InterruptedExceptione){//Ignore}}String[]pendingMessages=null;synchronized(messages){pendingMessages=messages.toArray(newString[0]);messages.clear();}try{if(connection==null){try{synchronized(this){wait();}}catch(InterruptedExceptione){//Ignore}}PrintWriterwriter=connection.getWriter();for(intj=0;j<pendingMessages.length;j++){finalStringforecast=pendingMessages[j]+"<br>";writer.println(forecast);log("Writing:"+forecast);}writer.flush();writer.close();connection=null;log("Closingconnection");}catch(IOExceptione){log("IOExeptionsendingmessage",e);}}}}}
js中的代码:functiongo(){varurl="/webChat/Weather";varxmlhttp=DefXmlHttp();xmlhttp.open("post",url,true);xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4){if(xmlhttp.status==200){if(xmlhttp.responseText){document.getElementById("forecasts").innerHTML=xmlhttp.responseText;}}go();}};xmlhttp.send(null);}
这里面的DefXmlHttp是另外封装的ajax请求请高手帮看看这问题是出在了什么地方,不胜感激!~~~
解决方案
解决方案二:
该回复于2011-03-03 15:14:17被版主删除