Tomcat7 implements Servlet3 asynchronous requests

Source: Internet
Author: User
Pom. xml: 1 & lt; dependency & gt; 2 & lt; groupId & gt; javax. servlet & lt;/groupId & gt; 3 & lt; artifactId & gt; javax. servlet-api & lt;/artifactId & gt; 4 & lt; version & gt; 3.0.1 & lt;/version pom. xml:
 
1

2
Javax. servlet
3
Javax. servlet-api
4
3.0.1
5
Provided
6

Web. xml:
 
1

2
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
3
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
4
Servlet3-Demo
5

AsyncServlet:
 
01
@ WebServlet (value = "/async-demo", asyncSupported = true)
02
Public class AsyncServlet extends HttpServlet {
03
 
04
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor (2 );
05
 
06
Public void doGet (HttpServletRequest req, HttpServletResponse res ){
07
AsyncContext aCtx = req. startAsync (req, res );
08
 
09
Executor.exe cute (new AsyncHandler (aCtx ));
10
}
11
 
12
}
AsyncHandler:
 
01
Public class AsyncHandler implements Runnable {
02
 
03
Private AsyncContext ctx;
04
 
05
Public AsyncHandler (AsyncContext ctx ){
06
This. ctx = ctx;
07
}
08
 
09
@ Override
10
Public void run (){
11
System. out. println ("Dispatch Time:" + System. currentTimeMillis ());
12
 
13
Ctx. dispatch ("/index. jsp ");
14
}
15
 
16
}


From Neron. L's blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.