Java Web Review Summary (6) - jsp - Directive - Include Directive

includeinstruction

grammar:

<%@ include %>
< jsp:include > 

@includeDirectives – executed during the translation phase

  @includeAny 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 directive JSP , then the JSP engine will JSP combine and translate the two into one servlet .

use:

<%@ include file="relativeURL"%>The fileproperties in it are used to specify the path of the imported file.

The path /starts with , indicating that it represents the current webapplication.

Note the issues:
1. The imported file must follow the JSPsyntax.
2. The imported file can have any extension, even if its extension ishtml

JSPThe engine will also process the content in it jspin the same way it processes the page (the tag element ). The
JSPspecification recommends using .jspf( JSP fragments(fragment)) as the extension for statically imported files.

3. Since the use of includeinstructions will involve 2 JSPpages, and the 2 pages will be JSPtranslated into a servlet, the instructions of these 2 JSPpages cannot conflict (except pageEncodingand 导包except).

Example:

  Create a new head.jspfpage and foot.jspfa page, which are used as jspthe head and tail of the page respectively, and are stored in WebRootthe following jspfragmentsfolders. The code is as follows:

head.jspfCode:

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 <h1 style="color:red;">网页头部</h1>

foot.jspfCode:

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 <h1 style="color:blue;">网页尾部</h1>

Create a page under   the WebRootfolder, import pages and pages using directives in the pageindex.jspindex.jsp@includehead.jspffoot.jspf

code show as below:

     <%-- 使用include标签引入引入其它JSP页面 --%>
     <%@ include file="/jspfragments/head.jspf" %>
     <h1>网页主体内容</h1>
     <%@ include file="/jspfragments/foot.jspf" %>

  Looking at the converted .javafile , it can be seen that the content of the page head.jspfand foot.jspfthe content of the page are out.writeoutput to the browser for display.

Note

  Use @includecan 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 @includeis called static inclusion, and the function is only to include the content of other pages.
  

jsp:includeLabels – 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 jspa type of action

advantage:

jsp:includeThe 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:includeContains URIthe , not the URIitself.
This means: the indicated URIis 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 Perlscript , Java servletor a CGIprogram , 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 includenever 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

includeusage of

If the site contains a few, if any, few , header , footer , and navigation files that rarely change , then the includebasic 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:includedusage of

However, for many Webapplications , 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 JSPtechnology -based sites also JSPfetch content from dynamic pages on other sites or applications.
If dynamic content is being processed, then it needs jsp:includeto 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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325745011&siteId=291194637