• Post Reply Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher include() call

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created two servlets A and B.
From A I forwarded to servlet B.In servlet B I call methods getContextPath() , getServletPath() , getRequestURI() , getQueryString() , getPathInfo() . And I get values as I have actually made a request to servlet B directly.
But when I replace forward() with include() all those methods that I called previously return the values for the request I made for servlet A .

Can someone put some more light why in case of include it behaves diffferently.We must be making a request to resource no matter forward or include.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is explicitly spelled out in the javadoc for RequestDispatcher.

For include:
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.
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you forward request to a resource
request attributes javax.servlet.forward.context_path , javax.servlet.forward.servlet_path , javax.servlet.forward.path_info , javax.servlet.forward.query_string , javax.servlet.forward.request_uri
return values for the original request
and when you use include
request attributes javax.servlet.include.context_path , javax.servlet.include.servlet_path , javax.servlet.include.path_info , javax.servlet.include.query_string , javax.servlet.include.request_uri
return values for the included resource

So they are completely opposite.
Similarly with methods getContextPath(), getServletPath() , getPathInfo() , getQueryString() , getRequestURI()

Am I right ?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not use the word "opposites".

Philosophically, a forward involves a change of responsibility: Servlet A is handing off the request to Servlet B and saying "Here, it's yours now. I don't want it back".

An include, on the other hand happens within the context of the first servlet. In this case, Servlet A is saying "Here, I want you to do some work on my request, but it's still mine, and give it back to me when you're done."
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Big thanks bibeault
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!