The difference between include directives and include actions in JSP

Source: Internet
Author: User

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, for the content that does not change frequently, it is appropriate to use include directive, if the content that need is changed frequently, need action element <jsp:include> the difference between them will be distinguished in detail below

1.include instruction
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 whether the contained pages have changed.

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

2.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= "sample.jsp" flush= "true" > <jsp:param name= "name" value= "value"/> </jsp:include >

Where the parameter settings can not, if there is no parameter setting, you must use <jsp:include page= "sample.jsp" flush= "true"/> Form

3. 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.

Since the include directive produces hard-to-maintain code, why should people use it?
Because the include directive is more powerful. The include directive allows the included files to contain JSP code that affects the main page, such as the settings of the response header and the definition of fields and methods.

Example://subpage.jsp
<%! int num=0; %>

mainpage.jsp
<body>
<%@ include file= "subpage.jsp"%>
<%= Num%>
</body>

Of course, this is not possible with the include action because the NUM variable is undefined and the main page cannot be successfully converted into a servlet.

The difference between an include directive and an include action in a JSP

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.