• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RequestDispatcher forward doubt?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic