The include command is a command in the compilation phase. That is, the content of the file contained in include is inserted into the JSP file during compilation. the JSP Engine judges that the JSP page has not been modified, otherwise, it is deemed to have been modified. Because the contained file is inserted during compilation, if only the content of the include file is modified but not the JSP file is not modified, the structure will not be changed, therefore, you can directly execute an existing bytecode file without re-compiling it. Therefore, it is appropriate to use the include command for infrequently changed content. If the required content changes frequently, the Action Element <JSP: Include> is required. the differences between them will be distinguished in detail below
1. IncludeCommand
Include can be used to convert JSP pages into servletsCodeInsert it. Its mainAdvantagesIs powerful. The contained code can contain JSP structures that affect the home page in general, such as attributes, method definitions, and document type settings. ItsDisadvantagesIt is difficult to maintain that the home page must be changed as long as the included page is changed, because the home page does not automatically check whether the included page has changed.
Syntax: <% @ include file = "sample. jsp" %>
2. IncludeAction
The JSP: include action contains the output of the secondary page when the homepage is requested. Although the output of the contained page cannot contain JSP, these pages can be the result of other resources. The server interprets the URL pointing to the contained resource in a normal way. Therefore, the URL can be a servlet or JSP page. The server runs the contained page in the usual way and places the generated output on the home page. This method is consistent with the include method of the requestdispatcher class. It does not need to be modified on the home page when the contained page is changed. Its disadvantage is that it contains the output of the secondary page, rather than the actual code of the secondary page, therefore, you cannot use any of the contained pages that may affect the JSP construction of the home page as a whole.
Syntax: <JSP: Include page = "sample. JSP "Flush =" true "> <JSP: Param name =" name "value =" value "/> </jsp: Include>
The parameter settings can be left blank. If no parameter is set, the format must be <JSP: Include page = "sample. jsp" Flush = "true"/>.
3.Use the include action or the include command?
Use the include command. If the included file changes, all the JSP pages that use it must be updated.
The include command should be used only when the include action cannot meet the requirements.
Some developers think that the code generated by the include command is executed faster than the code using the include action. Although this may be true in principle, the performance difference is very small, making it difficult to measure the workload. At the same time, the include action has a huge advantage in maintenance. When both methods can be used, the include action is almost certainly the preferred method.
Use the include action whenever possible for file inclusion. The include command should be used only when the fields or methods used on the home page are defined in the contained file, or when the contained file sets the response header on the home page.
Since the include command produces code that is difficult to maintain, why do people need to use it?
The include command is more powerful. The include command allows the contained files to contain JSP code that affects the home page, such as the settings of the response header and the definition of fields and methods.
Example:// Subpage. jsp
<%! Int num = 0; %>
// Mainpage. jsp
<HTML>
<Body>
<% @ Include file = "subpage. jsp" %>
<% = Num %>
</Body>
</Html>
Of course, this is impossible to use the include action, because the num variable is undefined and the Homepage cannot be converted to servlet.