What are the differences between method getRequestDispatcher(String path) and getNamedDispatcher(String path) in ServletContext Class?
NamedDispatcher
Returns a RequestDispatcher object that acts as a wrapper for the named servlet.
getNamedDispatcher(String path) method takes the name of the Servlet as parameter which is declared via Deployment descriptor.
Example
Deployment Descriptor
------------------------------------------------------------------------------------
servlet
servlet-name FirstServlet /servlet-name
servlet-class com.example.ServletExample /servlet-class
/servlet
------------------------------------------------------------------------------------
RequestDispatcher dispatch = request.getNamedDispatcher(“FirstServlet”);
dispatch.forward(request, response);
Note: A servlet instance can determine its name using servletConfig.getServletName(); This method returns the name of the class that implements HttpServlet class.
RequestDispatcher:
Returns a RequestDispatcher
object that acts as a wrapper for the resource located at the given path.
RequestDispatcher dispatch = request.getRequestDispatcher("/tes");
Here “/tes” represents the url-pattern element value of the servlet class.
servlet-mapping
servlet-name Test /servlet-name
url-pattern /tes /url-pattern
/servlet-mapping
We shouldn’t specify the entire path like
RequestDispatcher dispatch = request.getRequestDispatcher(str);
To forward a request to a jsp page we use
0 Comments:
Post a Comment
<< Home