[JSP] Differences between include directives and include actions in JSPs

Source: Internet
Author: User

The include directive is the compile-time directive, where the content of the files included in the include is inserted into the JSP file when compiling, and the JSP engine infers that the JSP page has not been altered, otherwise it is considered altered.

Because the included files are inserted at compile time. So assume that only the include file content is changed. Without changes to the JSP, the resulting structure will not change, so run the existing bytecode file directly. Without having to compile again. Therefore, it is appropriate to use the include directive for content that does not change frequently, assuming that the required content is often changed. It is necessary to <jsp:include> the action element. The following will specifically distinguish between them

1.include instruction
Include is able to insert JSP code into a JSP page before it is converted to a servlet.

Its main strength is its powerful features. The included code can contain JSP constructs that affect the main page as a whole, such as attributes, method definitions, and document type settings. The disadvantage is that it is difficult to maintain changes to only the pages that are included, so you have to change the main page, because the main page does not proactively see whether the included pages are 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 the included pages cannot contain JSPs, these pages can be the result of other resources.

The server interprets the URL that points to the included resource in a normal way. Thus the URL can be a servlet or JSP page. The server executes the included pages in the usual way. The resulting output is placed in the main page, in a way that is consistent with the include method of the RequestDispatcher class.

It has the advantage of not having to make changes to the main page when the included pages change. The disadvantage is that the output of the secondary page is included, not the actual code of the secondary page, so the included pages cannot be used regardless of the JSP constructs that may affect the master page in general.

Syntax: <jsp:include page= "sample.jsp" flush= "true" > <jsp:param name= "name" value= "value"/> </jsp:include >

If the parameter setting is not available, assume that there is no set of parameters, you must use <jsp:include page= "sample.jsp" flush= "true"/> Form


There are two main aspects of the difference;



One: Running time:

<%@ include file= "Relativeuri"%> is running in the translation phase
<jsp:include page= "Relativeuri" flush= "true"/> Run during the request processing phase.



Two: the introduction of different content:

<%@ include file= "Relativeuri"%>
Introduces static text (html,jsp), which is incorporated into a JSP page before it is transformed into a servlet.


<jsp:include page= "Relativeuri" flush= "true"/> Introduces the answer text generated by the run page or servlet.

III: Use the include action or the include directive?

Using the include directive, if the included file changes, then 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 feel that the code generated by the include directive runs faster than the code that uses the include action.

Although in principle it may indeed be so. But the difference in performance is so small that it's hard to measure, at the same time. The include action has a huge advantage in maintenance, and when both methods are available, the include action is almost certainly the preferred method.

For files included. Include actions should be used whenever possible. The include directive should be used only if the included files define the fields or methods to be used on the master page, or if the included files set 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 agrees that the included files contain JSP code that affects the main page. For example, the settings of the response header and the definition of fields, methods.


Examples://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 not defined. The main page cannot be successfully converted into a servlet.

[differences in include directives and include actions in jsp]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.