javascript
Brief description  about Online courses   join in Online courses
View Medha Vadi Saini 's Profile

Request be dispatched

Can Request be dispatched to another servlet rather than JSP?
Asked by Medha Vadi Saini | Jan 11, 2011 |  Reply now
Replies (1)
View Java Teacher 's Profile
Yes,
A request acn be dispatched to a HTML,jsp,servlet or any other resource.you can do it by following two types........
1) by getting the request dispatcher by getRequestDispatcher(); or by
2)getServleContext.getRequestDispatcher().

both of these accept one argument that is the path of the resource.
To illustrate, suppose you want Servlet_A to invoke Servlet_B. If they are both in the same directory, you could accomplish this by incorporating the following code fragment in either the service method or the doGet method of Servlet_A:


RequestDispatcher dispatcher = getRequestDispatcher("Servlet_B");
dispatcher.forward( request, response );

where request, of type HttpServletRequest, is the first parameter of the enclosing service method (or the doGet method) and response, of type HttpServletResponse, the second. You could accomplish the same by


RequestDispatcher dispatcher=getServletContext().getRequestDispatcher( "/servlet/Servlet_B" );
dispatcher.forward( request, response );


Jan 13, 2011