wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt on HFSJ Final Mock Exam #22 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt on HFSJ Final Mock Exam #22" Watch "Doubt on HFSJ Final Mock Exam #22" New topic
Author

Doubt on HFSJ Final Mock Exam #22

Xiaoxiao Lam
Greenhorn

Joined: Mar 04, 2006
Posts: 27
Hi,
Here is the question for Q22 of the final Mock Exam:

Given that /segment/footer.html is a static page, which JSP code would you use to include static content in a JSP?

A. <%@ include file="/segment/footer.html" %>
B. <jsp:forward page="/segment/footer.html" />
C. <jsp:include page="/segment/footer.html"/>
D. RequestDispatcher dispatcher=request.getRequestDispatcher("/segments/footer.html");
dispatcher.include(request,response);

The answers given are A and C. I understand why A and C are right. But my question is why D is not right? I want to know the exact reason. Is it because they should be inside <% %>? Or because HTML files can not be the argument of getRequestDispatcher()? (I tried it and it didn't work, but I don't understand because the document says it can include html files, jsp files and Servlets.) Thank you.


SCJP 1.4<br />SCWCD 1.4
Chandra Sagi
Ranch Hand

Joined: May 05, 2005
Posts: 162
Is it because they should be inside <% %>


Yes. That could be the only reason.

Thanks
Chandu
Xiaoxiao Lam
Greenhorn

Joined: Mar 04, 2006
Posts: 27
Thank you, Chandu. I supposed it would work if we put the code inside <% %>, but when I tried it using the exact code as choice D, it didn't work. I changed the file "footer.html" to "footer.jsp", it worked. Do you know why? Thank you.
Chandra Sagi
Ranch Hand

Joined: May 05, 2005
Posts: 162
Originally posted by Xiaoxiao Lam:
but when I tried it using the exact code as choice D, it didn't work. I changed the file "footer.html" to "footer.jsp", it worked.


Thats interesting, might be .html is considered static page and cannot be used using RequestDispatcher. Please confirm whether you have a .html page or .jsp in the correct location. If its working only with .jsp extension then I guess using RequestDispatcher we cannot include or forward to a static page. I would like to know the result too.

Thanks
Chandu
Chandra Sagi
Ranch Hand

Joined: May 05, 2005
Posts: 162
I am wrong. API says

include(ServletRequest request, ServletResponse response)
Includes the content of a resource (servlet, JSP page, HTML file) in the response.
it includes the contect of a HTML file too. So make sure you have a .html file

Thanks
Chandu
Xiaoxiao Lam
Greenhorn

Joined: Mar 04, 2006
Posts: 27
I am wrong. API says

it includes the contect of a HTML file too. So make sure you have a .html file


Yes, I had html file there when I used html file path and it didn't work. When I changed the html file to be a jsp file and used jsp file path, it worked.

Error Message when I used html file:

exception

org.apache.jasper.JasperException: getOutputStream() has already been called for this response
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:387)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802
[ March 14, 2006: Message edited by: Xiaoxiao Lam ]
Chandra Sagi
Ranch Hand

Joined: May 05, 2005
Posts: 162
Try these two options

1) strip the <html> and <body> tags incase you have some in your html file

2) See whether it works for dispatcher.forward(...)

I don't think you flushed the buffer or written anything to the response before dispatching it.

Thanks
Chandu
Xiaoxiao Lam
Greenhorn

Joined: Mar 04, 2006
Posts: 27
I didn't put any other content including "<html><body>" tags in the html file at all. Both include and forward method didn't work. And once I changed the file extension to be .jsp, both of the methods work.
 
Consider Paul's rocket mass heater.
 
subject: Doubt on HFSJ Final Mock Exam #22
 
Similar Threads
HFSJ final mock exam question
Doubt in Final Mock Exam:HFSJ
Include static content in a JSP
HFS Mack Exam Q22 - Page 772 - ResquestDispatcher - Is the answer correct?
HFSJ Final Mock exam - 5 doubts