File tree Expand file tree Collapse file tree 3 files changed +21
-10
lines changed
Expand file tree Collapse file tree 3 files changed +21
-10
lines changed Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <classpath >
3+ <classpathentry kind =" src" path =" src/test/java" output =" target/test-classes" including =" **/*.java" />
4+ <classpathentry kind =" src" path =" src/main/java" including =" **/*.java" />
5+ <classpathentry kind =" output" path =" target/classes" />
6+ <classpathentry kind =" var" path =" M2_REPO/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar" />
7+ <classpathentry kind =" con" path =" org.eclipse.jdt.launching.JRE_CONTAINER" />
8+ </classpath >
Original file line number Diff line number Diff line change 1515@ WebServlet (name ="asyncServlet" ,value = {"/async" },asyncSupported = true )
1616public class AsyncServlet extends HttpServlet
1717{
18- public static int index = 0 ;
1918
20- public AsyncServlet (){
21- System .out .println ("AsyncServlet - " +index ++);
22- }
19+
2320
2421private static final long serialVersionUID = 1L ;
2522
26- String param ="" ;
23+ String param ="" ;
24+
2725 @ Override
2826 protected void doGet (HttpServletRequest req , HttpServletResponse resp )
2927 throws ServletException , IOException {
3028
3129
30+ // 1.0 start async
3231 final AsyncContext ctx = req .startAsync ();
3332 param = ctx .getRequest ().getParameter ("seq" );
3433
35-
34+ // 2.0 set the timeout
3635 ctx .setTimeout (0 );
3736
37+ // 3.0 add listener
3838 ctx .addListener (new AsyncListener () {
3939
4040 @ Override
@@ -58,15 +58,15 @@ public void onComplete(AsyncEvent arg0) throws IOException {
5858 }
5959 });
6060
61+ // 4.0 run a thread
6162 ctx .start (new Runnable () {
6263 @ Override
6364 public void run () {
6465 try {
6566
66-
67- while (!param .equals ("exit" )){
68-
69- }
67+ // hold until receive exit
68+ while (!param .equals ("exit" )){}
69+
7070 ctx .getResponse ().getWriter ().write (ctx .getRequest ().getParameter ("seq" ));
7171
7272 } catch (IOException e ) {
Original file line number Diff line number Diff line change @@ -18,6 +18,9 @@ function sendAsync(seq) {
1818
1919$ ( document ) . ready ( function ( ) {
2020 $ ( "#start" ) . click ( function ( ) {
21+
22+ $ ( "tr:has(td)" ) . remove ( ) ;
23+
2124 for ( i = 1 ; i < 5 ; i ++ ) {
2225 $ ( "#asyncResponse" ) . append ( $ ( '<tr/>' )
2326 . append ( $ ( '<td/>' ) . text ( "request -" + i ) )
You can’t perform that action at this time.
0 commit comments