Huckle
Huckle

Reputation: 1940

Tree of RequestDispatcher.forward()

Suppose I am running a JSP/Servlet driven website. A user POSTs data to the root directory and I have a servlet (Index.java) that handles the path /. What I want this servlet to do is look at some of the data in the request and determine which of a second level of servlets should handle it. When it decides which second level servlet will handle the request, it forward()s to that second level servlet.

Now my problem is, what happens if a third level of servlet is needed? The second level of servlets cannot forward() to the third level because the dispatcher claims the response has already been committed.

Visually:

Index.java
 +SecondLevelA.java
 +SecondLevelB.java
 \SecondLevelC.java
   \ThirdLevelC.java

The SecondLevelC.java cannot forward to the third level. I also cannot redirect because I will lose the POST prameters in the redirect. POST is required because the data is too large for a GET query.

Upvotes: 1

Views: 109

Answers (1)

Ramesh PVK
Ramesh PVK

Reputation: 15456

The RequestDispatcher.forward() has nothing to do with the levels. It can be called any number of time hierarchically. The only condition is before any RequestDispatcher.forward() is called, the response should not be committed. It's true for first invocation as well.

In your case its most probably the first child servlet is causing the response to be committed.

Here are the reasons for response getting committed:

Causes of Response getting committed.

Upvotes: 1

Related Questions