The difference between <jsp:include> and <% @include file= ""%> (easy to understand)

Source: Internet
Author: User

Easy to understand

The include directive is the compile-time directive, where the content of the file included in the include is inserted into the JSP file when it is compiled, and the JSP engine determines that the JSP page has not been modified, otherwise it is considered modified. Because the included files are inserted at compile time, if only the content of the include file is modified without modification to the JSP, the resulting structure will not change, so the existing bytecode file is executed directly without recompiling. Therefore, it is appropriate to use the include directive for content that does not change frequently, and the action element <jsp:include> is required if the required content is constantly changing.

Include directives

Include can insert JSP code into a JSP page before it is converted to a servlet. Its main advantage is its powerful functionality, which contains code that can contain JSP constructs that affect the main page in general, such as attributes, method definitions, and document type settings. Its disadvantage is that it is difficult to maintain as long as the included pages change, you have to change the main page, because the main page does not automatically see if the contained pages have changed

Syntax: <%@ include file= "top.jsp"%>

Include action

The Jsp:include action is to include the output of the secondary page when the main page is requested. Although the output of a contained page cannot contain JSPs, these pages can be the result of other resources. The server interprets the URL that points to the contained resource in a normal way, so the URL can be a servlet or JSP page. The server runs the contained page in the usual way, putting the resulting output in the main page, in a way that is consistent with the RequestDispatcher class's include method. The advantage is that there is no need to make changes to the main page when the included pages change. The disadvantage is that it contains the output of the secondary page, not the actual code of the secondary page, so there is no JSP construct that can affect the main page in general on the included page.

Syntax: <jsp:include page= "top.jsp" flush= "true"/>

<jsp:include> and <% @include file= ""%> difference 1, in the execution time <%@ include file= "relativeuri"%> is executed during the translation phase
<jsp:include page= "Relativeuri" flush= "true"/> is executed during the request processing phase. 2. Introduction content of different <%@ include file= "relativeuri"%> introduced static text  Html,jsp, before the JSP page is converted into a servlet. <jsp:include page= "Relativeuri" flush= "true"/> Introduces the answer text generated by the execution page or servlet. 3, in the compilation of Jsp:include is to compile the included.jsp file first, and then include the first compile, and then include the @ include is the first to include the file, and then the unified compilation first, and then compile the plot difference

Is it OK to use the include action or the include directive?

With the include directive, if the contained file changes, all of its JSP pages need to be updated.

We should use the include directive only if the include action does not meet the requirements.

Some developers assume that the code generated by the include directive executes faster than the code that uses the include action. Although this may be true in principle, the performance difference is so small that it is difficult to measure, and the include action has a huge maintenance advantage, and when both methods are available, the include action is almost certainly the preferred method.

For file inclusion, you should use the include action whenever possible. The include directive should be used only if the containing file defines the field or method to use for the master page, or if the included file sets the response header of the main page.

Why do people still use it?

Because the include directive is more powerful. The include directive allows the included file to contain JSP code that affects the main page, such as the settings of the response header and the definition of the field, method

Summary: The <jsp:include page= "" > Parent page and the included page are compiled separately, translated into Servlets, and then spelled as an HTML page in the foreground. <% @include file= ""%> the parent page and the included page, after the code is merged, it is translated into a servlet and fed back to the foreground to form an HTML page.

The difference between <jsp:include> and <% @include file= ""%> (easy to understand)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.