反向Ajax之非同步Servlet
實現反向Ajax功能,除了WebSocket之外,還有Servlet3.0提供的非同步Servlet.
實驗透過Servlet3.0的非同步Servlet實現獲取伺服器端JVM資訊的功能。
http://blog.itpub.net/29254281/viewspace-1064838/
1.非同步Servlet程式設定asyncSupported為true,並返回組裝的JS程式碼。
2.前臺透過隱藏的iframe傳輸資訊
參考:
http://www.ibm.com/developerworks/cn/java/j-lo-comet/
實驗透過Servlet3.0的非同步Servlet實現獲取伺服器端JVM資訊的功能。
http://blog.itpub.net/29254281/viewspace-1064838/
1.非同步Servlet程式設定asyncSupported為true,並返回組裝的JS程式碼。
-
import java.io.IOException;
-
import java.io.PrintWriter;
-
import java.util.Date;
-
import java.util.Queue;
-
import java.util.concurrent.ConcurrentLinkedQueue;
-
import java.util.concurrent.Executors;
-
import java.util.concurrent.ScheduledExecutorService;
-
import java.util.concurrent.TimeUnit;
-
-
import javax.servlet.AsyncContext;
-
import javax.servlet.AsyncEvent;
-
import javax.servlet.AsyncListener;
-
import javax.servlet.ServletException;
-
import javax.servlet.annotation.WebServlet;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
@WebServlet(urlPatterns = { "/ac.do" }, asyncSupported = true)
-
public class InitServlet extends HttpServlet implements Runnable {
-
Queue<AsyncContext> queue = new ConcurrentLinkedQueue<AsyncContext>();
-
-
ScheduledExecutorService singleThread = Executors.newSingleThreadScheduledExecutor();
-
{
-
singleThread.scheduleWithFixedDelay(this, 3, 3, TimeUnit.SECONDS);
-
}
-
-
@Override
-
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
doGet(req, resp);
-
}
-
-
@Override
-
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
final AsyncContext ac = req.startAsync();
-
ac.setTimeout(10 * 60 * 1000);
-
ac.addListener(new AsyncListener() {
-
public void onComplete(AsyncEvent event) throws IOException {
-
queue.remove(ac);
-
}
-
-
public void onTimeout(AsyncEvent event) throws IOException {
-
queue.remove(ac);
-
}
-
-
public void onError(AsyncEvent event) throws IOException {
-
queue.remove(ac);
-
}
-
-
public void onStartAsync(AsyncEvent event) throws IOException {
-
}
-
});
-
queue.add(ac);
-
}
-
-
private String getJVMInfo() {
-
long useMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
-
useMemory = useMemory / 1024;
-
return "已用記憶體:" + String.valueOf(useMemory) + "K";
-
}
-
-
/**
-
* 這個函式太重要了! 返回JS程式碼執行.它呼叫父視窗的update方法
-
*/
-
private String htmlEscape(String message) {
-
return ";
-
}
-
-
@Override
-
public void run() {
-
System.out.println("傳送廣播通知");
-
String data = "廣播通知\n" + new Date() + "\n" + getJVMInfo();
-
for (AsyncContext ac : queue) {
-
try {
-
ac.getResponse().setContentType("text/html;charset=UTF-8");
-
PrintWriter writer = ac.getResponse().getWriter();
-
writer.println(htmlEscape(data));
-
writer.flush();
-
} catch (IOException e) {
-
queue.remove(ac);
-
e.printStackTrace();
-
}
-
-
}
-
}
- }
-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-
<title>WebSocket</title>
-
<script type="text/javascript"
-
src=""></script>
-
<script type="text/javascript">
-
$(document).ready(function() {
-
var url = './ac.do';
-
$('#comet-frame')[0].src = url;
-
});
-
-
function update(data) {
-
$('#content').html(data);
-
}
-
</script>
-
</head>
-
<body>
-
<div id="content"></div>
-
<iframe id="comet-frame" style="display: none;"></iframe>
-
</body>
- </html>
http://www.ibm.com/developerworks/cn/java/j-lo-comet/
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1065151/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 反向Ajax之WebSocketWeb
- 反向ajax技術
- 非同步Servlet非同步Servlet
- SpringMVC之ajax非同步互動SpringMVC非同步
- ajax請求servlet返回資料Servlet
- 網路IO之阻塞、非阻塞、同步、非同步總結非同步
- servlet非同步任務Servlet非同步
- AJAX同步和非同步區別非同步
- Servlet3.0新特性-非同步處理Servlet、FilterServlet非同步Filter
- jquery的ajax請求servlet與響應jQueryServlet
- 同步非同步,阻塞非阻塞非同步
- 非同步、同步、阻塞、非阻塞非同步
- 同步、非同步、阻塞、非阻塞非同步
- ajax的true非同步或者false同步非同步False
- AJAX 非同步請求非同步
- Let’s Hack非同步Servlet及Servlet3.0新特性非同步Servlet
- 同步非同步 與 阻塞非阻塞非同步
- 理解阻塞、非阻塞、同步、非同步非同步
- 同步、非同步,阻塞、非阻塞理解非同步
- 同步、非同步、阻塞與非阻塞非同步
- 同步、非同步、阻塞和非阻塞非同步
- Java之ServletJavaServlet
- AJAX 非同步(JavaScript 和 XMLHTTP)非同步JavaScriptXMLHTTP
- [轉]阻塞/非阻塞與同步/非同步非同步
- 同步與非同步 阻塞與非阻塞非同步
- AJAX資料互動中的同步非同步非同步
- 非同步Servlet學習筆記(一)非同步Servlet筆記
- java同步非阻塞IOJava
- 非同步和非阻塞非同步
- Ajax 資料非同步互動非同步
- AJAX的同步返回結果值
- 同步、非同步、阻塞、非阻塞的區別非同步
- JSP(ajax)+Servlet實現簡單的登入功能JSServlet
- layui使用html+servlet+ajax實現登入驗證UIHTMLServlet
- JavaScript 之 ajaxJavaScript
- 前端之AJAX前端
- Tomcat怎麼實現非同步ServletTomcat非同步Servlet
- oracle之 反向鍵索引Oracle索引