34248

Difference between request dispatcher forward and servlet chaining

Question:

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

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.

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

Recommend

  • Running multiple versions of a servlet web application parallel
  • Having difficulty working with global variables and $.getScript
  • How to return an object in my case?
  • Options for using Spring, Hibernate, JPA, and Tomcat with multiple Databases
  • Property Editor not registered with the PropertyEditorManager: error on custom tag invokation
  • Closest value different files, with different number of lines and other conditions ( bash awk other)
  • what is the diference between cascading and chaining [closed]
  • css background transition in opera goes through black
  • java.lang.ClassCastException: oracle.j2ee.ws.saaj.soap.TextImpl cannot be cast to javax.xml.soap.SOA
  • Regex takes a long time to complete
  • Customize google placepicker colors for android
  • Django: Serializing a list of multiple, chained models
  • Samsung Galaxy Tab 10.1 and -webkit-tap-highlight-color style?
  • c# linear regression given 2 sets of data
  • Extjs, handling success or failure when doing a standard submit in a form
  • Segmentation Fault on MySQL2 / Ruby 1.9.3 / Rails 3.2
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Does CSS support inheritance [duplicate]
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Eclipse MTJ doesn't see Java ME SDK 3.0 devices
  • Struts 2 TextField Tag with an attribute and no value
  • Image map in Flex
  • Can't delete or rename original file after resizing
  • Execute scripts AJAX returns
  • JBoss External Properties Files in Classpath
  • Assign variable to the value in HTML
  • Mysterious problem with floating point in LISP - time axis generation
  • Allowing both email and username for authentication
  • Seeking advice on Jetty HttpClient Hang
  • Using variable in a value field in jMeter
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Why is an OPTIONS request sent to the server?
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Align navbar back button on right side
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to add date and time under each post in guestbook in google app engine
  • Is there a mandatory requirement to switch app.yaml?
  • File upload with ng-file-upload throwing error
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • AngularJs get employee from factory