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?
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.
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 <% %>