• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about RequestDispatcher

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can not understand RequestDispatcher.

1. RequestDispatcher--include / forward. The books said " include ", the request is not "forwarded" permanently. INstead, it is passed to the other resource temporarily.

What is meaning of "temporarily"? , give me an example, please.

2. "RequesDispatcher.forward() is transparent to the browser while HttpServletResponse.sendRedirect() is not".

what is the meaning of "transparent"?.

Thanks
 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edward.


1. RequestDispatcher--include / forward. The books said " include ", the request is not "forwarded" permanently. INstead, it is passed to the other resource temporarily.
What is meaning of "temporarily"? , give me an example, please.

Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes. The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

The include method of the RequestDispatcher interface may be called at ANY time. The target servlet of the include method has access to all aspects of the request object, but its use of the response object is more limited. It can only write information to the ServletOutputStream or Writer of the response object and commit a response by writing content past the end of the response buffer, or by explicitly calling the flushBuffer method of the ServletResponse interface. It CANNOT set headers or call any method that affects the headers of the response. Any attempt to do so must be ignored.


2. "RequesDispatcher.forward() is transparent to the browser while HttpServletResponse.sendRedirect() is not".

It means that when you use HttpServletResponse.sendRedirect(), the browser is notified to go to a new location. Where as when RequestDispatcher.forward() is used, the client browser doesn't even know about it (i.e. it is transparent).
 
Pratibha Malhotra
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case of sendRedirect(), controll actually goes to browser and a new request is send to new servlet/jsp etc.

where as

in case of forward() same request is forwarded to new servlet/jsp etc

Hope this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic