Forums Register Login

RequestDispatcher include() call

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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.

 
+Pie Number of slices to send: Send
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 ?

+Pie Number of slices to send: Send
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."
+Pie Number of slices to send: Send
Big thanks bibeault
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 579 times.
Similar Threads
whats the difference between forward and redirect?
requestdispatch - servlet to servlet in iplanet( anyone plzzzzzzz!!!!)
javax.servlet.include. . .
doubt in servlet specification
page 494 HFSJ
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 20:16:35.