The difference between static include and dynamic include

Source: Internet
Author: User

the include in the JSP has two forms, namely:
<%@ include file= ""%>
<jsp:include page= "" flush= "true"/>
The former is the instruction element, the latter is the behavior element.
The include directive element of the <%@ include file= ""%>,jsp reads the contents of the specified page and merges them together (this process is done during the translation phase: that is, the JSP is transformed into a servlet), i.e., JSP pages cannot be passed to the browser intact, and all JSP elements must be processed by the server first. This is done by passing the JSP page into a servlet, Then execute the servlet. The server needs a JSP container to process the JSP pages. The JSP container is usually implemented as a servlet, which is configured to handle all requests to the JSP page .
The JSP container is responsible for translating the JSP page into a servlet and compiling the servlet. These two steps constitute the translation phase.
so we know that the JSP page is adding the actual content (that is, the code snippet) of the page specified by the include directive element to the JSP page that introduced it, When a file is synthesized, it is transformed into a servlet by the JSP container. You can see that a temporary class file and a Java file are generated.
<jsp:include page= "" flush= "true"/> the file is processed while the JSP page is running, and the processed file is logically and syntactically independent of the current JSP page. Compiled into different Java files
The following code tests the difference between dynamic and static include:

_01_include_01.jsp

<! DOCTYPE html>


_01_include_02.jsp

<! DOCTYPE html>/** Note: JSP tags need to close themselves, do not forget to close the horizontal line * jsp:include is dynamic loading, in the JSP compile time, loading the page and loaded page will be parsed two java,servlet files * javac The translator compiles two times, generates two class files * and then the JVM runs the process, this time contains the file to call the included class file * This is the dynamic inclusion **/%> <jsp:include page= "_02_date.jsp" flush= "true"/> <%/** As shown in the example above, because the build is two different servlet files--different class--> so each servlet has its own request object, not the same one. But the first one is to generate a servlet so it's the same */%> <%/** Different pages, different request can be transferred value * values do not forget to write the flush, where the value is also IO stream, need to flush * and then pass the value to the second page output*/%> <jsp:include page= "_02_date.jsp?username= ' 123 '" flush= "true"/> </font></body>

_02_date.jsp

<%@ page import= "java.util.*"%><%= (New Date ()). toLocaleString () + "<br/>"%><%= request + "--2--" + "<br/>"%><%= request.getparameter ("username") + "<br/>"%>


Results of running "_01_include_01":

From the results can be seen two request object is the same, the same object is not able to pass the parameter, and then look at the compiled Java, class file

You can see that there is only one Java and one class file.


Then run "_01_include_02",


From the results can also be seen two request object is obviously not the same, since it is not the same, it must be able to pass the reference, and then look at the compiled Java, class file


You can see that there are two Java files and corresponding class files.

Why is it?
Because the result of static include is the introduction of other JSPs into the current JSP, the two together as a whole
The structure of the dynamic include is both independent until the output is merged (see JSP generated Java file to know)
The following two images are illustrations of the difference between dynamic and static include


Static include plots:

Dynamic include plots:


The difference between static include and dynamic include

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.