Difference between the getRequestDispatcher and getNamedDispatcher in ServletContext?

The ServletContext class provides two methods getRequestDispatcher(String url-pattern) and getNamedDispatcher(String name), which can be used to dispatch a request to a particular servlet. The main difference between getRequestDispatcher() and getNamedDispatcher() method is that former accepts a URL pattern or path while later agrees with a Servlet name defined in deployment descriptor or web.xml file, both return an instance of RequestDispatcher class, which can further use to forward or redirect a request in Java web application. Let' see some more detail about these two methods and how they work.


getRequestDispatcher(String url-pattern)

This method is defined in ServletContext class and it returns a RequestDispatcher object that acts as a wrapper for the resrouce located at the given URL pattern or path, for example, if we have following servlet mapping in our deployment descriptor :

<servlet-mapping>
<servlet-name>InfoServlet</servlet-name>
<url-pattern>/info</url-pattern>
</servlet-mapping>

than

RequestDispatcher dispatch = request.getRequestDispatcher("/info");

will return RequestDispatcher for InfoServlet, here /info represents the url-pattern of the InfoServlet.

You can also use RequestDispatcher to forward a request to a JSP page, as shown below.

RequestDispatcher dispatch = request.getRequestDispatcher("/index.jsp");

Here path to index.jsp is relative to the root of the web application, that's why we have a forward slash prefix: "/index.jsp" to forward the request, just call dispatch.forward(request, response) where request and response are HttpServletRequest and HttpServletRespose object.

If you are interested to learn more about request forwarding in Servlet, you can further see Java Web Fundamentals By Kevin Jones on Pluralsight to learn more about HttpServlet class and other essential Servlet concepts.

Difference between the getRequestDispatcher and getNamedDispatcher in ServletContext?



getNamedDispatcher(String name)

This method is also defined inside ServletContext class, similar to previous method it also accepts a String argument but its not the path or URL pattern, instead its the name of the Servlet where you want to forwar the request. For example if we have following Servlet declaration in web.xml :

<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.message.HelloServlet</servlet-class>
</servlet>

then getNamedDispatcher("HelloServlet") will return a RequestDispatcher to forward request to our HelloServlet, as shown below:

RequestDispatcher dispatch = request.getNamedDispatcher("HelloServlet");
dispatch.forward(request, response

That's all about difference between getRequestDispatcher() and getNamedDispatcher() method of ServletContext class. Though both return an instance of RequestDispatcher to forward a request to some other Servlet or JSP, the former takes a URL pattern while later takes the name of the Servlet.


Further Learning
Java Web Fundamentals By Kevin Jones
Spring Framework 5: Beginner to Guru
JSP, Servlets, and JDBC for Beginners: Build a Database App


Other Servlet and JSP interview questions you may like
  • How DispatcherServlet works in Spring Framework? (answer)
  • Best books to learn Servlet and JSP (books)
  • Difference between ServletContext and ServletConfig in Servlet? (answer)
  • 6 Difference between forward() and sendRediret() in Servlet? (answer)
  • Difference between jsp:include and jsp:forward action in JSP? (answer)
  • 10 Servlet and JSP Interview Questions for Java Programmers (questions)
  • Difference between GenericServlet and HttpServlet in Servlet API? (answer)
  • How to manage client sessions in a Servlet application? (answer)
  • Difference between include directive and include action in JSP? (answer)
  • 5 Free Courses to learn Servlet and JSP in-depth (courses)
  • Difference between the web server, application server, and Servlet container? (answer)
  • Can you declare Constructor inside the Servlet class? (answer)
Thanks for reading this article so far. If you like these Servlet Interview questions, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note. 

P. S. - If you are new into Servlet and JSP and looking for a free tutorial or online course to learn basics, I suggest you go through Java Servlets and JSP - Build Java EE(JEE) app in 25 Steps - a free course on Udemy. It's completely free, all you need to do is create an Udemy account to enroll in this course.

No comments:

Post a Comment