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 (); }
}