Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding forwarding and including response from other Servlets

 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

As I was trying to understand RequstDispatcher, I fumbled down with some piece of code. Here below it goes. Please go through the code and I shall post my question after the code.

public class ReqDisServlet1 extends HttpServlet {

[Code having 2 buttons in a form - one for forward and one for include]

if(<when forward button ic clicked>) {
// Forwarding to Servlet2
System.out.println("Into the forwarding block");
}

if(<when include button ic clicked>) {
// Including Servlet2 response
System.out.println("Into the Including block");
}
}

---------------------------------------------------------------------------------------------------------------------------------------------
public class ReqDisServlet2 extends HttpServlet {
[Code which gets called from ReqDisServlet1]
}
---------------------------------------------------------------------------------------------------------------------------------------------

When the ReqDisServlet1 was executed with Tomcat, I had the following observation.
The two buttons, one for forwarding and one button for including were available in the screen.
When I clicked on the button for forwarding, the following output was seen in the tomcat console...
"Into the forwarding block"
Now, all that I have heard of RequestDispatcher.Forward is that, the control will be given to the called servlet and the control will not come back to the calling servlet. Is my understanding correct? Or is there any mistake in the code or its function?

I want clear understanding on Forward, Include and sendRedirect concepts.
Please explain.

Regards,
Sriram
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it.

all that I have heard of RequestDispatcher.Forward is that, the control will be given to the called servlet and the control will not come back to the calling servlet.

No this is not true, the control will return to the forwarding servlet but it cannot write anything to the client i.e. cannot send any response. It can do any other work like writing something to the console (as in your example)...
 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that response Ankit.
I have edited my posting. Will take care in future.

Regards,
Sriram
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using code tags, you have to wrap your code with [code] [/code] so that the code looks good
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic