Share on Google+Share on Google+

Servlet 3 Async Example

In this tutorial you will learn about the asyncSupported attribute in servlet.

Servlet 3 Async Example

In this tutorial you will learn about the asyncSupported attribute in servlet.

In servlet 3 an annotation is used for an alternative to the deployment descriptor. Two annotations @WebServlet and @WebFilter provides the feature of asynchronous processing using including an attribute asyncSupported. This is a new feature added in servlet 3.0 to support the servlet or servlet filter to use the asynchronous feature. If this attribute is set to the true then the servlet or servlet filter spports the asynchronous processing and if this attribute is not used or is set to false then the servlet or servlet filter will not support the asynchronous process.

Here I am giving a simple example which will demonstrate you about the use of this attribute in servlet. Here I have set the attribute asyncSupported to "true" in servlet and uses some methods of interfaces ServletRequest and AsyncContext.

Example :


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Asynchronous Servlet 3.0 Example</title>
<h3>Hi this is a demo example of Asynchronous Servlet 3.0 Example.</h3>

package roseindia.webContext;

import java.util.Date;
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

urlPatterns = "/Servlet3AsyncExample",
public class Servlet3AsyncExample extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException

Date date= new Date();
PrintWriter out= response.getWriter();

request.setAttribute("receivedAt", date);
AsyncContext asyncCtx = request.startAsync();
ServletRequest req = asyncCtx.getRequest();
boolean bol= req.isAsyncStarted();

//Will return true
out.println("<br>AsyncStarted : "+bol);

boolean bol1= req.isAsyncStarted();

//Will return false
out.println("<br>AsyncStarted : "+bol1);

boolean bol2= req.isAsyncSupported();

//Will return true
out.println("<br>AsyncSupported : "+bol2);

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doGet(request, response);

Output :

When you will execute the above example you will get the output as :

Download Source Code



Posted on: December 23, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Servlet 3 Async Example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image