Tomcat7实现Servlet3异步请求

 2023-09-13 阅读 26 评论 0

摘要:为什么80%的码农都做不了架构师?>>> pom.xml: <dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope>

为什么80%的码农都做不了架构师?>>>   hot3.png

pom.xml:

<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope>
</dependency>

js异步请求有哪几种方式。

web.xml:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name>Servlet3-Demo</display-name>
</web-app>

AsyncServlet:

@WebServlet(value = "/async-demo", asyncSupported = true)
public class AsyncServlet extends HttpServlet {ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);public void doGet(HttpServletRequest req, HttpServletResponse res) {AsyncContext aCtx = req.startAsync(req, res);executor.execute(new AsyncHandler(aCtx));}}

java接口异步不等待, AsyncHandler:

public class AsyncHandler implements Runnable {private AsyncContext ctx;public AsyncHandler(AsyncContext ctx) {this.ctx = ctx;}@Overridepublic void run() {System.out.println("Dispatch Time: " + System.currentTimeMillis());ctx.dispatch("/index.jsp");}}

转载于:https://my.oschina.net/neron/blog/39175

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/52941.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息