What is the difference between request dispatcher's forward method and the concept of servlet chaining?
RequestDispatcher rd= req.getRequestDispatcher("pathToServlet"); rd.forward(req,resp);
What this does is forwards the request without involving the client(browser) interaction. But can we achieve the same using <em>Servlet Chaining</em>?. If we can then what is the difference?.Answer1:
It's not different. "Servlet chaining" is just a term coined in dark J2EE 1.1/1.2 ages when servlet filters didn't exist. Indeed, it's basically the approach of using
RequestDispatcher#forward() to forward from one to other servlet (and ensuring that the response isn't already committed as that would otherwise result in
Since J2EE 1.3 (Servlet 2.3, over a <em>decade</em> ago already!) <a href="https://stackoverflow.com/tags/servlet-filters/info" rel="nofollow">servlet filters</a> were introduced which made the process so much more clean and easy. Since then, "Servlet chaining" is frowned upon and usually marked as "bad design". These days, you'd ultimately like to end up with only one front controller servlet and several business models.Answer2:
I think the concept of <strong>'chaining'</strong>, as it relates to configuring the server instead of using the
forward() method, is that you can configure certain types of requests to activate a particular chain of servlets.
For instance, if a request is from within an Intranet you might want the users to see some internal advertising. So you could have all these requests go through as
AddInternalBanner servlet first.
forward() method is useful if a particular servlet decides it should pass the request along.