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.

Servlet 3 Async Example

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 :

asyncExample.html

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

Servlet3AsyncExample.java

package roseindia.webContext;

import java.io.IOException;
import java.util.Date;
import java.io.PrintWriter;
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;

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

Date date= new Date();
response.setContentType("text/html");
PrintWriter out= response.getWriter();

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

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

asyncCtx.dispatch("/asyncExample.html");
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