Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher Doubt

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HFSJ P810 Q-22
Which JSP Code would you use to include static content in a JSP ;
D) RequestDispatcher dispatcher = request.getReuestDispatcher("/segments/footer.html");
dispatcher.include(request,response);

Why option D) is incorrect?
[ March 25, 2008: Message edited by: Sandeep Vaid ]
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not seen the book .But from your post it seems syntax problem : getReuestDispatcher:/segments/footer.html");
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was a typo. I have corrected it in the question..
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wouldn't use Requestdispatcher in a JSP page to include static content. You'd use the <jsp:include> tag. You should always avoid using Java code if you can use tags instead.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem here is that the include method of the RequestDispatcher will use ServletOutputStream or Writer of the response object. Unfortunately, there are good chances that your JSP file has already taken the ServletOutputStream or Writer. Trying to call getOutputStream or getWriter again will throw an IllegalStateException.

As Ulf mentioned, you should use <jsp:include> instead. This tag will use the "out" implicit object to print the included content.
[ March 27, 2008: Message edited by: Christophe Verre ]
 
Krishna Srinivasan
Ranch Hand
Posts: 1876
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.boot.by/wcd-guide/ch03s05.html
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this question says WOULD and not SHOULD.
I think it's asking whether can i use request dispatcher to include static file...
 
John Pearson
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought it was because the question specified JSP code in a JSP. The RequestDispatcher code shown would work fine in a servlet, but in a JSP it would have to be inside a scriptlet, which it's not.
 
Asif
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with john ..
Put <% %> around the code and get the rite ans.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic