Take the 2-minute tour ×

What is the difference between request dispatcher's forward method and the concept of servlet chaining?


RequestDispatcher rd= req.getRequestDispatcher("pathToServlet");

What this does is forwards the request without involving the client(browser) interaction. But can we achieve the same using Servlet Chaining?. If we can then what is the difference?.

share|improve this question
Dispatcher forward is one way to implement servlet chaining devx.com/DevX/Tip/26993 –  Nambari Dec 12 '12 at 16:59
add comment

2 Answers

up vote 3 down vote accepted

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 IllegalStateException).

Since J2EE 1.3 (Servlet 2.3, over a decade ago already!) servlet filters 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.

share|improve this answer
That means the best way to do it using forward method of RequestDispatcher right? –  Heggi Dec 12 '12 at 17:13
Even more, it's the only way. However, if you're already on a Servlet 2.3 capable container (I'd be really surprised if you're not), you should absolutely prefer using servlet filters for the job as you're basically abusing servlets as the wrong tool for the job. –  BalusC Dec 12 '12 at 17:15
Thank you @BalusC.. !!! –  Heggi Dec 12 '12 at 17:16
add comment

I think the concept of 'chaining', 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.

The forward() method is useful if a particular servlet decides it should pass the request along.

share|improve this answer
add comment

Your Answer


