• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RequestDispatcher include() call

 
Ranch Hand
Posts: 391
1
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Big thanks bibeault
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic