File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes forward vs include vs sendRedirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "forward vs include vs sendRedirect" Watch "forward vs include vs sendRedirect" New topic

forward vs include vs sendRedirect

Muralidhar Gandepalli

Joined: Apr 16, 2001
Posts: 15
Can some body answer these questions please
1)Can somebody compare RequestDispatcher.include(..), RequestDispatcher.forward(..) and HttpServletResponse.sendRedirect(..)?
2)For a servlet ,I feel that destroy() becomes a sort of redundant because we can use finalize() to achieve what ever we want to using destroy ? Please throw some light if iam missing something here....
Thanx in advance
[ May 27, 2003: Message edited by: Muralidhar Gandepalli ]
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 364
If you want to use the request or response object in the JSP you are forwarding to, then you'll need to use RequestDispatcher.forward()

No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

RequestDispatcher.include() processes the named resource and includes its output at the point of inclusion.
RequestDispatcher.forward() takes the entire current request environment and transfers it to the named resource. You cannot forward once output has been comitted.
HttpServletResponse.sendRedirect() causes a new request to be created by the client. The old request environment is not maintained. You also cannot redirect once output has been committed.
destroy() is called by the container when the servlet is unloaded. finalize() is part of garbage collection and cannot be counted upon to be called in any deterministic fashion. If your servelet must manage resources (close DB connections, for example) when shut down, you should rely upon destroy().

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: forward vs include vs sendRedirect
It's not a secret anymore!