Principle:
can see: http://yiminghe.javaeye.com/blog/294781
The advent of AJAX makes it possible for JavaScript to invoke the XMLHttpRequest object to make an HTTP request, and the JavaScript response handler updates the display of the HTML page based on the information returned by the server. Using AJAX to achieve "server push" differs from traditional AJAX applications in that:
- Server-Side requests are blocked until a data pass or timeout is returned.
- The client-side JavaScript response handler functions to re-establish the connection after processing the information returned by the server, making a request again.
- When the client processes the received data, re-establishes the connection, the server side may have new data arrival; The information is saved by the server until the client re-establishes the connection, and the client will retrieve all the information from the current server at once.
The code for the chat page:
Define Mm.js, define the Send message, define the JS function to receive the message
View Plaincopy to Clipboardprint?
- Ext.onready (function () {
- Getmsg ();
- });
- function getmsg () {
- Ext.Ajax.request ({url: "getmsg", Callback:function (options, success, response) {
- if (success) {
- Ext.DomHelper.append (Ext.get ("main"), Response.responsetext, true);
- }
- Getmsg ();
- }});
- }
- function Putmsg () {
- Ext.Ajax.request ({url: "putmsg", params:{message:document.getelementbyidx_x_x ("message"). Value}});
- }
Here is the servlet that gets the message
View Plaincopy to Clipboardprint?
- Package Hyjc.listener;
- Import java.io.IOException;
- Import Java.io.PrintWriter;
- Import javax.servlet.ServletException;
- Import Javax.servlet.http.HttpServlet;
- Import Javax.servlet.http.HttpServletRequest;
- Import Javax.servlet.http.HttpServletResponse;
- public class Getmsg extends HttpServlet {
- public void doget (HttpServletRequest request, httpservletresponse response)
- Throws Servletexception, IOException {
- Response.setcontenttype ("text/html");
- PrintWriter out = Response.getwriter ();
- Messagelist m = messagelist.getinstance ();
- Boolean end = false;
- while (!end) {
- System.out.println ("before get");
- String msg = M.get ();
- System.out.println ("After get" + msg);
- Out.write (msg + "
");
- if (M.isempty ()) {
- end = true;
- }
- }
- Out.close ();
- }
- public void DoPost (HttpServletRequest request, httpservletresponse response)
- Throws Servletexception, IOException {
- Doget (request, response);
- }
- }
Here is the servlet that adds the message
View Plaincopy to Clipboardprint?
- Package Hyjc.listener;
- Import java.io.IOException;
- Import Java.io.PrintWriter;
- Import javax.servlet.ServletException;
- Import Javax.servlet.http.HttpServlet;
- Import Javax.servlet.http.HttpServletRequest;
- Import Javax.servlet.http.HttpServletResponse;
- public class Putmsg extends HttpServlet {
- public void doget (HttpServletRequest request, httpservletresponse response)
- Throws Servletexception, IOException {
- Response.setcontenttype ("text/html");
- SYSTEM.OUT.PRINTLN ("put message");
- PrintWriter out = Response.getwriter ();
- Out.flush ();
- String msg = request.getparameter ("message");
- if (null! = msg) {
- Messagelist.getinstance (). Add (msg);
- } else {
- SYSTEM.OUT.PRINTLN ("Add message:" + msg + "results");
- }
- Out.close ();
- }
- public void DoPost (HttpServletRequest request, httpservletresponse response)
- Throws Servletexception, IOException {
- Doget (request, response);
- }
- }
Here is the message queue that holds the message, which is used internally with a blocking queue
View Plaincopy to Clipboardprint?
- Package Hyjc.listener;
- Import Java.util.Iterator;
- Import Java.util.concurrent.LinkedBlockingQueue;
- public class Messagelist {
- private static Messagelist list = null;
- private static Object key = new Object ();
- Private Messagelist () {
- This.add ("Hello");
- This.add ("World");
- }
- public static Messagelist getinstance () {
- Synchronized (key) {
- if (list = = null) {
- List = new Messagelist ();
- }
- return list;
- }
- }
- Private Linkedblockingqueue queue = new Linkedblockingqueue ();
- public Boolean isEmpty () {
- return Queue.isempty ();
- }
- public int size () {
- return Queue.size ();
- }
- Public String get () {
- try {
- return Queue.take ();
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- return null;
- }
- }
- public void Clear () {
- Queue.clear ();
- }
- public void Add (String msg) {
- try {
- Queue.put (msg);
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- }
- Public Iterator Iterator () {
- return Queue.iterator ();
- }
- }
Here is the demo effect, enter a message, click Submit, add to Messagelist, and then continue execution in getmsg for a long connection
Ajax-based long polling (long-polling) method for simple chat room programs