Saturday, July 9, 2016

What is request dispatcher and how many ways we can create an dispatcher object

Request Dispatcher :


Request Dispatcher is an interface which is having two methods forward(request, response) and include(request,response).

Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name.

This interface is intended to wrap servlets, but a servlet container can create RequestDispatcher objects to wrap any type of resource.

Simple explanation of request dispatcher is to forward control(request) to the next resource (like jsp,servlet or html) which has to do the processing of the request  and it will return the result to the end user or it can return the processed request to the calling function to fulfill the remaining processing of the request then the calling function will return the result to the end user.

We can create Request dispatcher in two ways

ServletContext context = getServletContext();
                 RequestDispatcher dispatcher = context.getRequestDispatcher("/processing.jsp");
                 dispatcher.forward(request,response);
RequestDispatcher requestDispatcher; 
requestDispatcher = request.getRequestDispatcher("/thankYou.jsp");   
requestDispatcher.forward(request, response); 
requestDispatcher.include(request,response);

No comments:

Post a Comment