Servlet3.0新特定之 --- Servlet异步支持

Servlet3.0新特定之--- Servlet异步支持
一、Servlet异步支持简介1.1、为什么要用异步处理

​ Servlet是用来处理客户端发送的请求和给客户端响应数据的业务逻辑的。如果,Servlet在处理用户请求数据的逻辑中调用了一个比较耗时的方法,势必会影响到给客户端响应数据。Servlet3.0为我们提供了一个异步支持,这样我们就可以把比较耗时的逻辑放在异步的方法中进行处理(相当于重新开启了一个线程来处理这个耗时的业务逻辑)。这样,就不会影响到给客户端响应数据了。

1.2、异步实现的原理

重新开一个线程单独去调用耗时的业务方法。

二、Servlet异步支持具体实现2.1、在@WebServlet注解中添加异步支持**

@WebServlet(name = "MyAsyncServlet",urlPatterns = "/asyncServlet",asyncSupported = true)

2.2、调用异步方法

步骤:

1、通过request活的AsyncContent对象:AsyncContext actx = request.startAsync();

2、设置异步调用超时时长:actx.setTimeout(30*3000);

3、启动异步调用线程:actx.start(new MyThread(actx));

1 //通过request获得AsyncContent对象

2  AsyncContext actx = request.startAsync(); //重点方法**

3 //设置异步调用超时时长

4 actx.setTimeout(30*3000);  

5 //启动异步调用的线程

6 actx.start(new MyThread(actx));//重点方法**

2.3、应用实例

【java代码】

1 package com.heima.servlet;

2 ​

3 import javax.servlet.AsyncContext;

4 import javax.servlet.ServletException;

5 import javax.servlet.ServletRequest;

6 import javax.servlet.annotation.WebServlet;

7 import javax.servlet.http.HttpServlet;

8 import javax.servlet.http.HttpServletRequest;

9 import javax.servlet.http.HttpServletResponse;

10 import java.io.IOException;

11 import java.io.PrintWriter;

12 ​

13 /**

14  * @author buguniao

15  * @date 2018-06-25 10:46

16  */

17 @WebServlet(name = "MyAsyncServlet", urlPatterns = "/asyncServlet", asyncSupported = true)

18 public class MyAsyncServlet extends HttpServlet {

19     @Override

20     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

21         //解决乱码

22         request.setCharacterEncoding("UTF-8");

23         response.setContentType("text/html;charset=UTF-8");

24         //通过request获得AsyncContent对象

25         AsyncContext actx = request.startAsync();

26         //设置异步调用超时时长

27         actx.setTimeout(30 * 3000);

28         //启动异步调用的线程

29         actx.start(new MyThread(actx));

30 ​

31         //直接输出到页面的内容(不等异步完成就直接给页面)

32         //但这些内容必须放在标签内,否则会在页面输出错误内容,这儿反正我测试是这样,具体不知对不对??

33         PrintWriter out = response.getWriter();

34         out.println("<h1>不等异步返回结果就直接返到页面的内容</h1>");

35         out.flush();

36     }

37 ​

38     /**

39      *异步处理业务的线程类

40      */

41     public class MyThread implements Runnable {

42         private AsyncContext actx;

43 ​

44         //构造

45         public MyThread(AsyncContext actx) {

46             this.actx = actx;

47         }

48 ​

49         @Override

50         public void run() {

51             try {

52                 //等待5秒,模拟处理耗时的业务

53                 Thread.sleep(4 * 1000);

54                 //获得request对象,添加数据给页面

55                 ServletRequest req = actx.getRequest();

56                 req.setAttribute("content", "异步获得的数据");

57                 //将请求dispath到index.jsp页面,该页面的session必须设为false

58                 actx.dispatch("/index.jsp");

59             } catch (Exception e) {

60                 e.printStackTrace();

61             }

62         }

63     }

64 ​

65     @Override

66     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

67         doGet(req, resp);

68     }

69 }

【index.jsp】

1 <%--

2   Created by IntelliJ IDEA.

3   User: buguniao

4   Date: 2018/6/25

5   Time: 10:59

6   To change this template use File | Settings | File Templates.

7 --%>

8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>

9 <html>

10 <head>

11     <title>Title</title>

12 </head>

13 <body>

14         ${content}

15 </body>

16 </html>

评论

暂无评论