Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Servlets and the fly likes sendRedirect() Vs. include() Vs. forward() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect() Vs. include() Vs. forward()" Watch "sendRedirect() Vs. include() Vs. forward()" New topic

sendRedirect() Vs. include() Vs. forward()

Rajasekhar Karanam

Joined: Sep 18, 2011
Posts: 16

Guys, Please make me clear about these topics in Servlets.
ntumba lobo
Ranch Hand

Joined: Oct 21, 2008
Posts: 180

sendRedirect(myUrl1) : you are sending back a response to the client and asking the browser to make a new request with the provided url, here myUrl1

include() : Say in servletA you call include(/path/to/myView.jsp) : you are asking the web container to include in the response of servletA the response of myView.jsp.
ServletA is in control, I mean responsible for generating the response to the client and you just include as well the response of another web component (e.g jsp)

forward() : say in servletA you call forward(path/to/myView.jsp): here servletA passes the control to myView.jsp to generated the response to the client. The client will see only the content from myView.jsp nothing from servletA.

I hope that helps

I agree. Here's the link:
subject: sendRedirect() Vs. include() Vs. forward()
It's not a secret anymore!