简体   繁体   中英

One of the difference between JSP include directive and JSP include tag

Accodign to the JSP specification:

A JSP container can include a mechanism for being notified if an included file changes, so the container can recompile the JSP page. However, the JSP 1.2 specification does not have a way of directing the JSP container that included files have changed.

And I found this in an on-line tutorial:

If the included file is changed but not the JSP which is including it then the changes will reflect only when we use include action tag. The changes will not reflect if you are using include directive as the JSP is not changed so it will not be translated (during this phase only the file gets included when using directive) for request processing and hence the changes will not reflect.

then, I write an example to try it : A.jsp, B.jsp, and C.jsp,

A.jsp includes C.jsp using JSP include directive:

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

B.jsp includes C.jsp using JSP include tag:

<jsp:include page="/jsp/C.jsp" />

and when I access A.jsp and B.jsp in the borwser, thay both display normally, then I changed C.jsp, and I refeshed A.jsp and B.jsp, both of them can display the change of C.jsp. but if the JSP spec is true, the A.jsp shouldn't display the change of C.jsp. is there anything wrong?

First we shall take this,

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

  1. include directive will paste the page as it is in the place where this statement is used.
  2. If you have made changes in your static content either its in A.jsp or C.jsp , it will be rendered with the changes that you made.

<jsp:include page="/jsp/C.jsp" />

  1. This include action tag is used to insert either a static or dynamic content.
  2. So the changes will reflect as it is ie the C.jsp will executed by passing the request and response objects to it and include the executed page in the A.jsp .

EDIT :

For example,

In B.jsp ,

<c:out value="${value}"/>

In A.jsp ,

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="value" value="10"/>

<span>From @include <%@include file="B.jsp" %></span>
<span>From jsp:include <jsp:include page="B.jsp" /></span>

When the page is loaded it will be,

From @include 10
From jsp:include

if you inspect and see, it will look like,

<span>From @include 10</span>
<span>From jsp:include <c:out value/></span>

This is because, @include will replace the contents of B.jsp into A.jsp before the rendering starts, where as for jsp:include , A.jsp and B.jsp will execute separately.

As the taglib is not imported in B.jsp , the c:out tag is not executed.

Upon changing B.jsp to

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:out value="${value}"/>

if you inspect and see the rendered page, it will look like,

<span>From @include 10</span>
<span>From jsp:include </span>

variable value has its scope only in A.jsp .

In jsp:include

scope is page

In @include

scope is request

The actual text on this topic from the JSP specification is:

A JSP container can include a mechanism for being notified if an included file changes, so the container can recompile the JSP page. However, the JSP 1.2 specification does not have a way of directing the JSP container that included files have changed.

The same text appears unaltered (apart from the specification version) in all subsequent versions of the specification to the current one (2.3 at the time of writing).

Most JSP containers will detect the change in the included file and trigger a re-compilation when the including file is next requested.

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

do insert content dynamically, suppose you include C.jsp in you A.jsp , so if you have declared variable in C.jsp you can refer it in you A.jsp at compile and run time.

but if <jsp:include page="/jsp/C.jsp" /> you have done this,

then your content of C.jsp will come in A.jsp (like copy and paste)(at run time only).

after this if you change content of C.jsp then you need to again recall refresh of A.jsp.

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