What is the defferent between getNamedDispatcher() and getRequestDispatcher()?
There are three ways to obtain RequestDispatcher object:
-
RequestDispatcher dispatch = request.getRequestDispatcher(arg);wherearg(a string) can be a relative path or not to a particular resource. (i.e. jsp, servlet, etc.) It may be both a relative (not starting with '/') or relative (to this request) URL. -
RequestDispatcher dispatch = getServletContext.getRequestDispatcher(arg);wherearg(a string) must be an absolute URL (starting with a '/') targeting a valid servlet or jsp file. This URL being associated to the servlet with a <servlet-mapping> element in the DD "/string" must NOT be a relative path. It must start with a forward slash "/". - RequestDispatcher dispatch = getServletContext.getNamedDispatcher("string"); This method must take the servlet name as parametere. The servlet name is definded in web.xml as <servlet-name>. The request is dispatched to that corresponding servlet. arg2 must be the name of a servlet, as given by the <servlet-name> sub-element of the <servlet> element of the DD.
The mainly difference between these methods is that the "getNamedDispatcher()" method from (ServletContext interface) does not add some request "attributes" to the request operation. It means that the included or forwarded page are not able to get some attributes from the request sent.
The getRequestDispatcher() methods sets the folloing request attributes while transfering the request in case of include:
javax.servlet.include.request_uri
javax.servlet.include.servlet_path
javax.servlet.include.context_path
and it sets the following request attributes while transfering the request in case of forward:
javax.servlet.forward.request_uri
javax.servlet.forward.servlet_path
javax.servlet.forward.context_path
javax.servlet.forward.query_string
NamedDispatcher
Returns a RequestDispatcher object that acts as a wrapper for the named servlet.
getNamedDispatcher(String) 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 the Servlet interface or extends the 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>
It represents the path of the servlet class. Since both the base as well as target servlet are in the same package structure by just specifying the url-pattern element value we will be able to access the target servlet.
We shouldn't specify the entire path like
String str = "/WEB-INF/classes/com/example/posr/Test"
RequestDispatcher dispatch = request.getRequestDispatcher(str);
To forward a request to a JSP page we use
RequestDispatcher dispatch = request.getRequestDispatcher("/TestJspOne.jsp");
Here "/TestJspOne.jsp" the slash denotes the Jsp page is at the root of the application.
Most Recent servlet Faqs
- What is the getInputStream() of ServletRequest for?
- How to get the client information in a Servlet?
- Can I control Session timeout?
- What is the difference between RequestDispatcher's forward method and HttpServletResponse's sendRedirect method?
- When do I use HttpSessionListener?
- What is the difference between the request attribute and request parameter?
- What is the defferent between getNamedDispatcher() and getRequestDispatcher()?
Most Viewed servlet Faqs
- What is the difference between the request attribute and request parameter?
- When do I use HttpSessionListener?
- How to use ServletContext.getResourceAsStream(java.lang.String path)?
- What is the difference between RequestDispatcher's forward method and HttpServletResponse's sendRedirect method?
- Can I control Session timeout?
- How to get the client information in a Servlet?
- What is the defferent between getNamedDispatcher() and getRequestDispatcher()?