• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher forward doubt?

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I have 2 servlets,


When I call ServletA, I'm getting the o/p as
Now in ServletB
Back in ServletA

How come??
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

Yes, this doesn't seem right. What implementation (or app. server) are you using? Here's what the spec (2.3) says (SRV.8.4):

"Before the forward method of the RequestDispatcher interface returns, the
response content must be sent and committed, and closed by the servlet container."
[ April 30, 2007: Message edited by: Leandro Melo ]
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leandro,

I got to know how this works...Even after doing a rd.forward(request, response);, we are eligible to write to the console...check the line, I'm actually doing System.out.println() and I'm not writing to the response. Anyone to augment my discussion??
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jothi.

Yes, you're right. As the spec says, the response must be commited, but the method return is made anyway. That's why writing to the console works.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are both right.

I also wanted to add that this is not the case when using the <jsp:forward> action. If you take a look at the translated .java file of a JSP page that uses that standard action you'll see that the Container inserts a return after the RequestDispatcher.forward() method call. This complies with what the JSP spec says:


A <jsp:forward page=�urlSpec� /> action allows the runtime dispatch of the current request to a static resource, a JSP page or a Java servlet class in the same context as the current page. A jsp:forward effectively terminates the execution of the current page.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergio,

So does that mean that when using a RequestDispatcher.forward, the control is still within the service method that contains this code?? It is only that we are giving away the response to some other handler??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic