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 :
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 :

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: ServletRequest Java EE 6
Post your Comment