Turn polling, long polling, IFrame long connection, web socket

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.