ServletRequest Java EE 6

In this section we have discussed about the changes made in ServletRequest interface in Java 6.

ServletRequest Java EE 6

In this section we have discussed about the changes made in ServletRequest interface in Java 6.

ServletRequest Java EE 6

ServletRequest Java EE 6

In this section we have discussed about the changes made in ServletRequest interface in Java 6.

There is no changes made in the ServletRequest interface definition. As you aware with the definition of ServletRequest that is the ServletRequest is an interface reference of which is created by the servlet container is passed in the argument of a Servlet's service method to provide the request information made by a client to a servlet. All the sub interfaces of ServletRequest interface provides their additional information.

There are various of new methods are added in ServletRequest. Some of these are as follows :

  • getAsyncContext() : This method is used to find AsyncContext.
  • getDispatcherType() : This method is used to find the DispatcherType of the request that is used by the container to choose a filter required to apply on the request.
  • getServletContext() : This method is used to find the ServletContext for the last dispatched request.
  • isAsyncStarted() : This method is used to check whether the request is in asynchronous mode or not.
  •  isAsyncSupported() : This method is used to check whether the request is supported asynchronous operation or not.
  • startAsync() : This method is used to make the request in asynchronous mode.
    • startAsync() : This method is used to make the request in asynchronous mode and the AsyncContext is instantiated by their base ServletRequest and ServletResponse objects i.e. objects are unwrapped.
    • startAsync(ServletRequest request, ServletResponse response) : This method is also used to make the request in asynchronous mode and in this method AsyncContext is instantiated by their specified request and response objects i.e. objects are wrapped.

Example :

servletrequest.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>Login Page</title>
</head>
<body>
<form>
<br>
<b>Newly added methods example of ServletRequest in Java 6.</b>
</form>
</body>
</html>

ServletRequestExample.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 = "/ServletRequestExample",
asyncSupported=true
)
public class ServletRequestExample extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

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

AsyncContext asyncCtx = request.startAsync();
ServletRequest req = asyncCtx.getRequest();
AsyncContext a= request.getAsyncContext();
out.println("AsyncContext = "+a);
boolean bol= req.isAsyncStarted();

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

asyncCtx.dispatch("/servletrequest.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