Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:39:06+00:00 2026-06-01T12:39:06+00:00

I come from a Java SE background, and I did some servlets tutorials and

  • 0

I come from a Java SE background, and I did some servlets tutorials and read Head First JSP & servlet. I’m reading the JavaWorld.com article about Async support now but I don’t quite understand it.

What’s Async is simply about ?
What is the difference between Ajax and Servlet Async?

P.S I have a PHP background with ajax and I know the concept, but I haven’t tried it with java

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:39:07+00:00Added an answer on June 1, 2026 at 12:39 pm

    In the traditional Servlet model, it’s normally the case that 1 request corresponds to 1 thread.

    These threads typically come from a pool which is managed by the Servlet container. The Servlet container can only handle new requests as long as it has free threads in this pool. As long as your own code is busy processing the request, the thread is not free.

    In some situations it might be worth it to break this model. What happens is that a request arrives at the Servlet via such a Servlet container managed thread, and your code then asks for asynchronous execution. You can then return from the Servlet request and the container thread will be freed.

    Contrary to synchronous request processing, this will not commit any response and will not close the connection. Instead, you can hand the async context off to another thread pool, which can pick it up, and when some thread is free to handle it, service it and will be able to write to the response.

    An example:

    @WebServlet(urlPatterns = "/somepath", asyncSupported = true)
    public class AsyncServlet extends HttpServlet {
    
        @EJB
        private AsyncBean asyncBean;
    
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync();
    
            // The following line will not block and return immediately
            asyncBean.doAsyncStuff(asyncContext);
    
        } // Shortly after this method has ended, thread will be returned to pool
    }
    

    With the AsyncBean being implemented as:

    @Stateless
    public class AsyncBean {
    
        @Asynchronous
        public void doAsyncStuff(AsyncContext asyncContext) throws IOException {
            asyncContext.getResponse().getWriter().write("test");
        }
    }
    

    In the code above, somewhere shortly after you return from the AsyncServlet#doGet() method, the Servlet thread will be returned to the pool. A ‘request’ (task) for executing AsyncBean#doAsyncStuff() will have been put in the queue for the EJB thread pool to pick up.

    The answer to WHY and WHEN you would use this is not that straightforward. If you just want to preserve threads then in the above case you would be exchanging one a thread from one thread pool for the other (in this case the Servlet pool vs the EJB async pool) and the net benefit wouldn’t be that much. You could just as well have given your Servlet thread pool an extra thread.

    In more advanced scenarios, you could however do more fine-grained managing of requests; divide them into multiple tasks and have a thread pool service those tasks. E.g. imagine 100 download requests for a 10MB file handled by 10 threads that round-robin give sends 100KB a time to each request.

    Yet another application is a request that needs to wait for data from an external system, and where this external system is capable of sending a message that can be relayed back to the requestor. I.e. a database call would not make sense here, since you would need another thread waiting for the response anyway. Then you would change one thread for another again. But if you need to wait for say an incoming email, then one thread can wait for any email and relay that to any suspended request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've come from a java background, so i'm still getting to grips with some
I come from a Java background and with any servlets-based technology, it's trivial to
I come from Java Background and so used to Debugging using Eclipse but have
I come from a Java background and have been using C# for the last
I'm new to PHP but come from a Java background and I'm trying to
I come from Java background, where we have data structures with interfaces that if
I am new to C++ (just starting). I come from a Java background and
I come from a Java/Eclipse background and I fear that I am spoiled by
I've been iPhone programming for 6 months and come from a PC/Java/Eclipse background and
I come from a Java background where the web application is always resident in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.