• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher for including static contents

 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can RequestDispatcher be used to include static contents ?
Q 22 in final Mock of K&B had this option but option was not selected.
 
Ashok Kumar Babu
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Request dispatcher cannot be used to include or forward static contents like html.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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



Thanks
 
Ashok Kumar Babu
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Narendra,



Please tell me whether the above code is valid? Or will it call the intend page?
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Nitin Vashishtha
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Narendra,

In your JSP. It will run without error.


Tried, but does not work, the same exception occurs
 
Ashok Kumar Babu
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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



Found some discussion on Sun Forum

Thanks
 
Joshua Antony
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the above code works!
 
Pedro Erencia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic