Difference between include directive and tag

The include instruction is an instruction in the compilation phase, that is, the content of the file included in the include is inserted into the JSP file when compiling, and the JSP engine judges that the JSP page has not been modified, otherwise it is deemed to have been modified. Since the included file is inserted at compile time, if only the content of the include file is modified without modifying the JSP, the resulting structure will not change, so the existing bytecode file is directly executed without rewriting compile. Therefore, for the content that does not change frequently, it is appropriate to use the include directive. If the required content changes frequently, the action element <jsp:include> is required. The difference between them will be distinguished in detail below. 1. The include


directive
    include can be found in JSP code is inserted into the JSP page before it is converted into a servlet. Its main advantage is that it is powerful, and the code it contains can contain JSP constructs that affect the main page as a whole, such as attribute, method definition, and document type setting. Its disadvantage is that it is difficult to maintain that whenever the included page changes, the main page has to be changed, because the main page does not automatically see if the included page has 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 included pages cannot contain JSPs, these pages can be the result of other resources. The server interprets the URL pointing to the contained resource in the normal way, so this URL can be a servlet or a JSP page. The server runs the included page in the usual way, placing the resulting output on the main page, in the same way as the include method of the RequestDispatcher class. It has the advantage of not having to modify 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 you cannot use any JSP constructs in the included page that might affect the main page as a whole.


Use include action or include directive?   Using the include directive, if the included file changes, then all Jsp pages that use it need to be updated.   We should use the include directive only when the include action does not satisfy the requirements.














































  Some developers believe that the code generated by the include directive executes faster than the code that uses the include action. While this may be true in principle, the difference in performance is so small that it's hard to measure, and the maintenance advantage of the include action is huge, and when both methods are available, the include action is almost certainly the preferred method.


  For file inclusion, the include action should be used whenever possible. The include directive should only be used if the included file defines a field or method to be used by the main page, or if the included file sets the main page's response headers.


Why do people use the include directive when it produces hard-to-maintain code?
  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 setting of response headers 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 not possible when using the include action, because the num variable is undefined and the main page cannot be successfully converted into a servlet.

Guess you like

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