Ajax implements web long connection __ajax

Source: Internet
Author: User
Tags button type

This article is well written and has benefited greatly from the long connection of web communications, long polling (polling). 1. What is long polling, long connection

In plain words, the client keeps sending requests to the server to get the latest data. The "Non-stop" here is actually stopped, but our human eyes can not tell whether to stop, it is just a quick stop and then immediately began to connect.
2. My application Scenario

Want to do a two-dimensional code check-in system.
Main requirements:
-Publish sign-in on the browser
-One two-dimensional code can only be used once
-Automatically refreshes the new two-dimensional code 3 when the two-dimensional code displayed on the browser is used . Specific Code page:

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>  
 Server-Side   
Package com.servlet;

Import Com.service.QRService;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import java.io.IOException;
Import Java.io.PrintWriter; public class Qrajax extends HttpServlet {protected void DoPost (HttpServletRequest request, HttpServletResponse Respon
    SE) throws Servletexception, IOException {doget (request,response);  } protected void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException
        {HttpSession session=request.getsession ();
        Qrservice service=new Qrservice ();
        PrintWriter out = Response.getwriter ();
        timing int time=0;
                if (session!=null) {//Long polling while (true) {time++;
            Determine if two-dimensional code has been used if (Service.isreflash ()) {        Out.print ("No" +session.getattribute ("RecordCount") + "");
                Break
                        }else{//Hold 10 sec if (time>=10) {Out.print ("failed");
                    Break }else {try {//Hold, 1-second cycle Thread.Sleep (10
                        00);
                        catch (Interruptedexception e) {e.printstacktrace ();
    }}}} out.close (); }
}
Related Article

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.