简体   繁体   中英

Something about JSP include directive and JSP include tag

In the JSP Spec 2.1, I found an example about the JSP include directive and JSP include tag:

For an example of a more complex set of inclusions, consider the following four situations built using four JSP files: A.jsp, C.jsp, dir/B.jsp and dir/C.jsp :

在此处输入图片说明

I don't quite understand this, especially the first and last situation, why the C.jsp are not resolved to C.jsp in the first situation? and why the c.jsp are not resolved to dir/c.jsp in the last situation?

include directive

Use this directive to specify a resource that contains text or code to be inserted into the JSP page when it is translated.

For example:

<%@ include file="/jsp/userinfopage.jsp" %>

Specify either a page-relative or context-relative path to the resource.

See Requesting a JSP Page for discussion of page-relative and context-relative paths.

Notes:

  • The include directive, referred to as a static include, is comparable in nature to the jsp:include action discussed later in this chapter, but jsp:include takes effect at request-time instead of translation-time. See Static Includes Versus Dynamic Includes .

  • The include directive can be used only between files in the same servlet context (application).


In a JSP include directive the path can be relative to the including page or absolute (then it has to start with a / and belongs to the web application root directory).

For more info have a look at another post here

Please have a look at Including Content in a JSP Page

  • The directive <%@ include> fills in the text of an evaluation of the included JSP with the <%@> directives evaluated. As such the nested include is evaluated with respect to directory of the included JSP.
  • The tag <jsp:include> calls a compiled version of the included jsp.

So a single JSP is evaluated for text on its own, triggering the evaluation on includes first. Now it should be clear.

First case:

  1. evaluate directives - A.jsp, include dir/B.jsp
  2. evaluate directives - dir/B.jsp, ... include C.jsp = dir/C.jsp
  3. generate java for A.jsp, with text of dir/B.jsp with text from dir/C.jsp.

<%@ include file= ... > is executed while the page is translated into a servlet class. Thus, it knows the context of the file system. This is also called static inclusion, and the tag is a direction.

<jsp:include page= ...> is executed while the page is executed for a request. This is also called dynamic include, and the tag is an action. Here, the context of the file system is unknown.

Static inclusion means that a chunk of code lines is copied into the resulting final file. Dynamic inclusion means that the included page is executed, and the result is copied into the resulting final file.

Here are good sources:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM