• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Doubt on HFSJ Final Mock Exam #22

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is it because they should be inside <% %>



Yes. That could be the only reason.

Thanks
Chandu
 
Xiaoxiao Lam
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
What? What, what, what? What what tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic