Servlet3.0新特性---异步支持
来源:互联网 发布:mindmap软件 编辑:程序博客网 时间:2024/06/09 20:06
1.servlet2.5中,页面发送一次请求,是顺序执行,即使在servlet里的service中开启一个线程,线程处理后的结果是无法返回给页面的,servlet执行完毕后,response就关闭了,无法将后台更新数据即时更新到页面端
2.现在后台“推”是怎么实现的
2.1定时发送请求,页面有刷新,不好友
2.2Ajax 轮询,然后通过js更新页面数据
相比前者虽然友好,访问量太大时,服务器会增加压力,小型应用可以考虑用
2.3反向Ajax(Comnet)
利用Http1.1长连接的特性,也是通过轮询,但是每次发送请求不会立即返回,而是等待服务器有数据时才返回或者没有等到数据而连接超时返回,相比于ajax轮询,减少了服务端压力,但一个缺点,不是所有浏览器都支持。
3.servlet3.0中提供了异步支持,当数据返回页面后,request并没有关闭,当服务器端有数据更新时,就可以推送了
4.更多关于web异步,请参考这边文章 http://blog.csdn.net/cenwenchu79/article/details/5703430
5.测试servlet3.0异步
SecondServlet.java
- package com.darren.servlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Date;
- 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;
- /**
- * servlet3.0默认是不支持异步的通过asyncSupported=true,打开
- *
- */
- @WebServlet(name="SecondServlet",urlPatterns={"/secondServlet"},asyncSupported=true)
- public class SecondServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- PrintWriter out = null;
- resp.setContentType("text/html");
- try {
- out = resp.getWriter();
- out.print("servlets starts:"+new Date()+"<br>");
- out.flush();
- AsyncContext asyncContext = req.startAsync();
- /**
- * AsyncListener为什么没有适配器呢?需要各个厂家实现?
- */
- asyncContext.addListener(new AsyncListener(){
- public void onComplete(AsyncEvent asyncEvent) throws IOException {
- //将流在这里关闭
- asyncEvent.getSuppliedResponse().getWriter().close();
- System.out.println("asynContext finished....");
- }
- public void onError(AsyncEvent arg0) throws IOException {
- // TODO Auto-generated method stub
- }
- public void onStartAsync(AsyncEvent arg0) throws IOException {
- // TODO Auto-generated method stub
- }
- public void onTimeout(AsyncEvent arg0) throws IOException {
- // TODO Auto-generated method stub
- }
- });
- new Thread(new MyThread(asyncContext)).start();
- out.print("servlets ends:"+new Date()+"<br>");
- out.flush();
- } finally {
- /**
- * 一开是在这里关闭了,关了,后面就用不成了 :)
- *
- */
- /*if(null != out){
- out.close();
- out = null;
- }*/
- }
- }
- }
MyThread.java
- package com.darren.servlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Date;
- import javax.servlet.AsyncContext;
- public class MyThread implements Runnable {
- private AsyncContext asyncContext;
- public MyThread(AsyncContext asyncContext) {
- this.asyncContext = asyncContext;
- }
- public void run() {
- PrintWriter out = null;
- try {
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- out = asyncContext.getResponse().getWriter();
- out.println("myTask starts:"+new Date()+"<br>");
- out.flush();
- out.print("myTask ends:"+new Date()+"<br>");
- out.flush();
- asyncContext.complete();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- /*if(null != out){
- out.close();
- out = null;
- }*/
- }
- }
- }
主要是这个对象AsyncContext,参考J2EE6 API吧http://docs.oracle.com/javaee/6/api/
0 0
- Servlet3.0新特性---异步支持
- Servlet3.0新特性---异步支持
- [Servlet3.0新特性]Servlet异步处理
- servlet3.0新特性Servlet3.0引入的若干重要新特性,包括异步处理、新增的注解支持、可插性支持等等
- 【Servlet3.0新特性】第01节_Servlet注解及异步支持
- Servlet3.0---异步支持
- Servlet3.0异步支持
- servlet3.0 新特性
- servlet3.0新特性
- Servlet3.0新特性
- servlet3.0新特性
- Servlet3.0新特性
- Servlet3.0新特性
- Servlet3.0新特性
- Servlet3.0新特性
- Servlet3.0新特性
- servlet3.0新特性
- Servlet3.0新特性
- oc学习笔记(九)ARC和block类型
- 十五周 项目1 工资数据的输入
- Java的注解机制——Spring自动装配的实现原理
- 苹果的Swift语言
- css兼容问题(一)
- Servlet3.0新特性---异步支持
- C/C++之回调函数
- css兼容问题(三)
- 转: Maven反应堆
- 简要分析汽车的车速受限是由于哪几方面的原因
- 广州传智播客.Net学科基础班,就业班升级啦!
- 分享2D Unity游戏的动画制作经验
- 十五周项目二
- 【项目1-工资数据的输入】 文件读入工资