include
instruction
grammar:
<%@ include %>
< jsp:include >
@include
Directives – executed during the translation phase
@include
Any file can be included, but only the contents of the file are included.
-
include
Directive imports are also commonly referred to as static imports . -
If other pages are introduced using the
include
directiveJSP
, then theJSP
engine willJSP
combine and translate the two into oneservlet
.
use:
<%@ include file="relativeURL"%>
The file
properties in it are used to specify the path of the imported file.
The path /
starts with , indicating that it represents the current web
application.
Note the issues:
1. The imported file must follow the JSP
syntax.
2. The imported file can have any extension, even if its extension ishtml
JSP
The engine will also process the content in itjsp
in the same way it processes the page (the tag element ). The
JSP
specification recommends using.jspf
(JSP fragments
(fragment)) as the extension for statically imported files.
3. Since the use of include
instructions will involve 2 JSP
pages, and the 2 pages will be JSP
translated into a servlet, the instructions of these 2 JSP
pages cannot conflict (except pageEncoding
and 导包
except).
Example:
Create a new head.jspf
page and foot.jspf
a page, which are used as jsp
the head and tail of the page respectively, and are stored in WebRoot
the following jspfragments
folders. The code is as follows:
head.jspf
Code:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<h1 style="color:red;">网页头部</h1>
foot.jspf
Code:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<h1 style="color:blue;">网页尾部</h1>
Create a page under the WebRoot
folder, import pages and pages using directives in the pageindex.jsp
index.jsp
@include
head.jspf
foot.jspf
code show as below:
<%-- 使用include标签引入引入其它JSP页面 --%>
<%@ include file="/jspfragments/head.jspf" %>
<h1>网页主体内容</h1>
<%@ include file="/jspfragments/foot.jspf" %>
Looking at the converted .java
file , it can be seen that the content of the page head.jspf
and foot.jspf
the content of the page are out.write
output to the browser for display.
Note
Use @include
can contain arbitrary content, the suffix of the file does not matter .
This kind of statement that includes the content of other files into its own page @include
is called static inclusion, and the function is only to include the content of other pages.
jsp:include
Labels – Executed during the request processing phase. (Will be discussed later in the labeling section)
<jsp:include page=”relativeURI” flush=”true” />
Called dynamic include
(dynamic containment), it is jsp
a type of action
advantage:
jsp:include
The advantage is: changes in included files are always checked. The difference from include first
and then compile is to use first compile, then include@include
jsp:include
principle:
jsp:include
Contains URI
the , not the URI
itself.
This means: the indicated URI
is interpreted and thus the resulting response is included. If the page is HTML
, then will get nothing changed at all HTML
. However, in the case of a Perl
script , Java servlet
or a CGI
program , the result will be interpreted from the program.
While the page usually is HTML
, the actual program happens to be a means to an end.
Also, since the interpretation is done each time the page is requested , the result is include
never cached as it is with the directive .
While this is a small change, it makes all the difference in the behavior you're seeing.
Summarize
Mixed use
include
usage of
If the site contains a few, if any, few , header , footer , and navigation files that rarely change , then the include
basic directive is the best option for these components. Because
directives are cached , you only need to put an include file once and its contents will be cached, which can result in greatly improved site performance.include
jsp:included
usage of
However, for many Web
applications , a blanket cache is not the answer. While headers and footers may be static, it is impossible for the entire site to be static.
For example, navigation links are fetched from databases, external calls to others api
, and many JSP
technology -based sites also JSP
fetch content from dynamic pages on other sites or applications.
If dynamic content is being processed, then it needs jsp:include
to be handled by .
The best solution is to often mix and match the two approaches, using each construct where it works best.
difference between the two
<%@ include file=""%>
Instructions are generally placed at the top when writing code, the main function is to facilitate the use of reused code
<jsp:include page="" flush="" />
When a label imports a file, the file is compiled and imported, which essentially contains the compiled content of the file to be imported , so it can contain duplicate content with the currentjsp
program , (it will be parsed before it is included. Lose)