It's not a secret anymore!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes RequestDispatcher for including static contents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher for including static contents" Watch "RequestDispatcher for including static contents" New topic
Author

RequestDispatcher for including static contents

Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254
Can RequestDispatcher be used to include static contents ?
Q 22 in final Mock of K&B had this option but option was not selected.


SCJP,SCWCD, Into ATG now!
Ashok Kumar Babu
Ranch Hand

Joined: Jul 25, 2006
Posts: 129
Request dispatcher cannot be used to include or forward static contents like html.


Ashok<br /> <br />SCJP 91%<br />SCWCD 88%
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

RequestDispatcher can be used to include/forward for both static and dynamic contents.



Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Ashok Kumar Babu
Ranch Hand

Joined: Jul 25, 2006
Posts: 129
Hi Narendra,



Please tell me whether the above code is valid? Or will it call the intend page?
Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254
Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254
Got access to a machine today only... ran the check code to include static content, getting the below exception



However it works fine for invoking dynamic content
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi Ashok,

I do not find anything wrong with your code. It is perfectly valid.The code will display the contents of included_page.html.

Hi Joshua ,

Can you place the full code. I seen that you are trying to forward the request when some contents are already written in the output stream.

Thanks
[ April 07, 2008: Message edited by: Narendra Dhande ]
Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254
Nitin Vashishtha
Ranch Hand

Joined: Mar 26, 2008
Posts: 31
Originally posted by Ashok Kumar Babu:


Please tell me whether the above code is valid? Or will it call the intend page?


Above code is perfectly valid and it will forward the request to included_page.html.
request dispatcher is to dispath the current request to any other resource,which you can access from a direct request from your browser,static or dymanic does not matter.


Nitin
SCJP 91%, TOFAG 9 certified 89%
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi Joshua,

In case of JSP file you are writing lot of html code into the JSP that generate contents to the outputstream. Try only following code.

In your JSP. It will run without error. Try your original code using forward it should run.

But I think in case of include this problem is due to the app server select PrintWriter for writing the html in JSP and the dispatcher is using the outputstream. Both can be used at same time.

In case of forward , If there is enough buffer in output and the response is not commited, the contents will be flushed and request is forwarded to new resource.

Thanks
[ April 07, 2008: Message edited by: Narendra Dhande ]
Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254
Hi Narendra,

In your JSP. It will run without error.


Tried, but does not work, the same exception occurs
Ashok Kumar Babu
Ranch Hand

Joined: Jul 25, 2006
Posts: 129
Hi Narendra,

I tried including a static image file.



It works fine. It concludes well that RD can include and forward both static and dynamic contents. But still I did not get the point why it does not work in case of html which has some contents.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

This code are working fine fro tomcat and glassfish for include.



Found some discussion on Sun Forum

Thanks
Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254
Yes, the above code works!
Pedro Erencia
Ranch Hand

Joined: Apr 03, 2008
Posts: 70
Hi Ashok,

I tried including a static image file.

code:

<% request.getRequestDispatcher("time.bmp").forward(request,response); %>



It works fine.


I thought you can't include binary content in a forward/include fashion if you don't set the contentType before as corresponding and use getOutputStream. By default contentType is "text/html".. Now i remember a HFSJ question on that, though i don't have the book now and can't remember the chapter.

Can someone clarify about this ?

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher for including static contents