Skip to content

marekgregor/async-servlet-with-filter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

async-servlet-with-filter

This project demonstrates asynchronous Servlet 3 filters.

Run the application in a Tomcat container or directly with Jetty

> ./gradlew runJetty

and open http://localhost:8080

Description

  1. First the filter is called with dispatch type REQUEST
  2. An AsyncContext is started
  3. A asynchronous request (to http://checkip.amazonaws.com) is done and the doFilter method exits
  4. When the callback on the asynchronous call returns the dispatch() is called on the AsyncContext
  5. The request is dispatched again through the doFilter method, this time with dispatch type ASYNC
  6. This time we call chain.doFilter() and the request enters the servlets doGet() method
  7. A new AsyncContext is started and a asynchronous dispatch to a JSP page is done on an other thread
  8. The servlets doGet() method exits and control is returned to the filter that registers an AsyncListener
  9. The onStartAsync() is called on the AsyncListener
  10. The filters doFilter() method exits
  11. The response from the jsp is returned

Printout is something like this in Tomcat 8: (In Jetty 9 the last row is missing...)

WaitFilter::doFilter: >>> Start
Filter::doFilter: [REQUEST] Start >>> (Thread[http-nio-8080-exec-6,5,main])
Filter::doFilter: [REQUEST] End <<< (Thread[http-nio-8080-exec-6,5,main])
Filter::doFilter: <<< End 
Filter::doFilter: [REQUEST] AsyncResponse <<< (Thread[New I/O worker #1,5,RMI Runtime]), attr=81.170.155.156 
Filter::doFilter: >>> Start
Filter::doFilter: [ASYNC] before doFilter >>> (Thread[http-nio-8080-exec-7,5,main])
Servlet::doGet: Start >>> (Thread[http-nio-8080-exec-7,5,main])
AsyncListener::onStartAsync(Thread[http-nio-8080-exec-7,5,main])
Servlet::doGet: End <<< (Thread[http-nio-8080-exec-7,5,main])
Filter::doFilter: [ASYNC] after doFilter <<< (Thread[http-nio-8080-exec-7,5,main])
Servlet::In Async(Thread[pool-1-thread-1,5,RMI Runtime])
Filter::doFilter: <<< End 
Hello in JSP (Thread[http-nio-8080-exec-8,5,main]) attr=81.170.155.156 
AsyncListener::onComplete(Thread[http-nio-8080-exec-8,5,main])

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 92.8%
  • HTML 7.2%