• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFS Mack Exam Q22 - Page 772 - ResquestDispatcher - Is the answer correct?

 
Justin
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
22. Which JSP code would you use to include static content in a JSP?

a. <%@ include file="/segments/footer.html" %>
b. <%@ forward page="/segments/footer.html" %>
c. <%jsp:include page="/segments/footer.html" %>
d. RequestDispatcher dispatcher = request.getRequestDispatcher("/segments/footer.html");
dispatcher.include(request,response);

The answer for the above question was given as (a) and (c)

My question is - Why is (d) not a correct answer too?

If not why?
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Dispatcher that obtained via ServletRequest, the URL must be RELATIVE PATH. Thus, D is not correct.

For Dispatcher that obtained via ServletContext, the URL could be either relative or absolute.

Nick
 
Roger Yates
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took a more simplistic approach.
For d. to be correct it would need to be part of a scripting section:

though as Nick pointed out, it wouldn't be valid even if it was as above!
 
Neil Mc
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought there may have been an error with this question as well, but for a different reason.

I thought that the only correct answer should be (a) i.e. NOT (c) as a <jsp:include> is used to include dynamic content and the question distinctly asks how you should include static content.
 
pallavi utukuri
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think C is definetely correct as:
<jsp:include>:
1.The <jsp:include> element allows you to include either a static or dynamic resource in a JSP page.
2.If the resource is static, its content is included in the calling JSP page.
3.If the resource is dynamic, it acts on a request and sends back a result that is included in the JSP page

comming to option D:
RequestDispatcher from request :
1.The pathname specified may be relative, although it cannot extend outside the current servlet context.
2.If the path begins with a "/" it is interpreted as relative to the current context root.
3.If the path does not start with "/" its considered relative to original request.
4.The resource can be dynamic or static.

soo A,C and D are correct answers....
am i ryt?
 
Shiang Wang
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Relative path can start with '/' or without.
When it starts with "/", it means that it is relative to the context root.
When it starts without "/", it means that it is relative to the current servlet or jsp.
But RequestDispatcher you get via ServletContext must start with "/", or you will get this exception.
org.apache.jasper.JasperException: Path included.jsp does not start with a "/" character
RequestDispatcher you get via ServletRequest can start with or without "/" depends on what you need. Therefore the answer D is invalid not because of the path, but the missing <% %>

Shiang
 
pallavi utukuri
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oooh i missed out that! good point
 
Sudhakar Namasivayam
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this correct ??? <%jsp:include page="/segments/footer.html" %>


Should it not be <jsp:include page="/segments/footer.html"/>
 
pallavi utukuri
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yaa this is wrong! <%jsp:include page="/segments/footer.html" %>
it must b <jsp:include page="/segments/footer.html" />
i must b blind not to see that

so that means C is not a Correct option
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic