简体   繁体   中英

Difference between request dispatcher forward and servlet chaining

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

Example

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 Servlet Chaining ?. If we can then what is the difference?.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM