• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

RequestDispatcher doubt :(

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got horriably confused while reading about the RequestDispatcher.

1. what is the difference between
ServletContext.getRequestDispatcher("url"). AND ServletContext.getNamedDispatcher(""). I guess they both are used to get the RequestDispatcher Object?

2. where can we used include() and where forward(). what are the differnces. Is include() include the content of the includede servlet at request time?

3. The following request attributes must be set before calling include() and forward()
javax.servlet.forward.request_uri
javax.servlet.forward.context_path
javax.servlet.forward.servlet_path
javax.servlet.forward.path_info
javax.servlet.forward.query_string

Is this create any difference if they will set by the include() OR forward() method.

4. request_uri and servlet_path attributes are behave same??

please help as I read the specification also but not get my doubt cleared

Thanks
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Poonam Agarwal wrote:1. what is the difference between
ServletContext.getRequestDispatcher("url"). AND ServletContext.getNamedDispatcher(""). I guess they both are used to get the RequestDispatcher Object?



Well as the name suggests, getNamedDispatcher gets a request dispatcher to a servlet or jsp based on the name defined in the web.xml file. I am referring to this part of the web.xml file



While the getRequestDispatcher method gets a request dispatcher based on the URL mapping in the web.xml file.

Poonam Agarwal wrote:2. where can we used include() and where forward(). what are the differnces. Is include() include the content of the includede servlet at request time?



Include is used to, well, include the output of another servlet into the current response while forward is used to assign the task of generating the response to the forwarded resource. If any response is committed before calling forward, then an exception is throws. And yes include is used to include the response of another servlet at request time. You can forward to a servlet or JSP but I'm not sure if we can include the output of a JSP.



Poonam Agarwal wrote:3. The following request attributes must be set before calling include() and forward()
javax.servlet.forward.request_uri
javax.servlet.forward.context_path
javax.servlet.forward.servlet_path
javax.servlet.forward.path_info
javax.servlet.forward.query_string

Is this create any difference if they will set by the include() OR forward() method.



Well first of all we don't have to set these attributes. They are automatically set by the servlet container. Also the name of the attributes is different for include and forward.







Poonam Agarwal wrote:4. request_uri and servlet_path attributes are behave same??




well request_uri returns the same value as request.getRequestURI() and servlet_path returns the same value as request.getServletPath()...
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Poonam,

To answer some of your queries :

1. getNamedDispatcher returns a RequestDispatcher which takes the name of the servlet mentioned in the <servlet-name> in the DD
whereas the other one takes the <url-pattern> element from the DD. For more info, refer to this : http://faq.javaranch.com/java/ServletsFaq#6

2. include, in the case of servlets, is more like you send the request & response objects to some other servlet to perform some operations on them and then return them back to your calling servlet for further processing on these objects if so required. Also, in include, there are restrictions on the use of the response object like you are not allowed to write headers to the response object and you can write information only to the ServletOutputStream or Writer of the response object. Whereas in forward, these restrictions do not apply.

3. Although I dont have exact idea about this in the case of include, but my guess is that you dont set these attributes explicitly just like you dont set set them in the case of forward. The container takes care of it. Moreover, these attributes dont create a difference in the method call, its the method call itself which tells the container what to do.

4. For a better understanding of include and forward and the request attributes, refer this : http://www.perhome.com/resin-doc/webapp/faq.xtp

Hope this helps.
 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sourin, please help me to explain this secenario,



The value of request.getQueryString(), however, depends on whether an include() or a forward() was done.

for example.. for include index.jsp

called with url /testapp/index.jsp?foo=original

index.jsp contains <jsp:include page="target.jsp"/>

1.request.getQueryString() in target.jsp returns foo=original
2.request.getParameter("foo") in target.jsp returns original
3.request.getAttribute("javax.servlet.include.query_string")
in target.jsp returns null


-------------------------------------------------------------------------------------------------------------
called with url /testapp/index.jsp?foo=original
index.jsp contains <jsp:foward page="target.jsp"/>

1.request.getQueryString() in target.jsp returns null
2.request.getParameter("foo") in target.jsp returns original
3.request.getAttribute("javax.servlet.include.query_string")
in target.jsp returns foo=original



getting mad
 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please guide me to understand this concept
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Poonam Agarwal wrote:
called with url /testapp/index.jsp?foo=original
index.jsp contains <jsp:foward page="target.jsp"/>
:
3.request.getAttribute("javax.servlet.include.query_string")
in target.jsp returns foo=original



but my book says that in case of a forward, the attribute name is javax.servlet.forward.query_string and not javax.servlet.include.query_string. Strange !!!
 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it just a typo.... that's it, But still i am not getting my answer
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic