Citation: http://www.cnblogs.com/AloneSword/p/3517463.html
Http://www.cnblogs.com/wei2yi/archive/2011/03/23/1992830.html
code example:
1.iframe for long connections
<%@ Page Language="Java"ContentType="text/html; charset=gb18030"pageencoding="GB18030"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=gb18030"><title>Insert Title here</title></Head><Scripttype= "Text/javascript"src= "Jquery-1.9.1.js"></Script><Script> functionLoadData () {$ ('#logs'). Append (NewDate ()); }</Script><Body><iframeID= "Frame"name= "Polling"src= "/myweb/testlongpolling"></iframe> <DivID= "Logs"></Div></Body></HTML>
PackageBookstore;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Servlet Implementation class Testlongpolling*/@WebServlet ("/testlongpolling") Public classTestlongpollingextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; /** * @seeHttpservlet#httpservlet ()*/ Publictestlongpolling () {Super(); //TODO auto-generated Constructor stub } /** * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response)*/ protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method StubDoPost (Request,response); } /** * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response)*/ protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method Stub inti = 0; BooleanBoo =true; String result=NULL; while(boo) {Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } result= "Hello I ' m server" +i;//characters to print to the foreground//sendmsg (Result,response, "msg");//msg is the name of the JS method in test.jspResponse.setcontenttype ("TEXT/HTML;CHARSET=GBK"); Response.getwriter (). Write ("<script type= ' Text/javascript ' >parent.loaddata () </script>"); Response.flushbuffer (); I++; if(i==10) {Boo=false; } } }}
2.web socket
Cond....
Turn polling, long polling, IFrame long connection, web socket