aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes RequestDispatcher doubt :( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher doubt :(" Watch "RequestDispatcher doubt :(" New topic
Author

RequestDispatcher doubt :(

Poonam Agarwal
Ranch Hand

Joined: May 12, 2008
Posts: 323
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


Thanks <br />Poonam Agarwal SCWCD 86%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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()...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sourin K. Sen
Ranch Hand

Joined: May 02, 2006
Posts: 86

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.

Regards,<br />Sourin.
Poonam Agarwal
Ranch Hand

Joined: May 12, 2008
Posts: 323
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

Joined: May 12, 2008
Posts: 323
please guide me to understand this concept
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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

Joined: May 12, 2008
Posts: 323
it just a typo.... that's it, But still i am not getting my answer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher doubt :(
 
Similar Threads
Doubt in RequestDispatcher concept of servlets
A question on RequestDispatcher
doubt in servlet specification
Doubt on RequestDispatcher
*****ServletRequest attributes doubt*******