The difference between dynamic include and static include in JSP

Source: Internet
Author: User

Dynamic include uses Jsp:include action to implement <jsp:include page= "included.jsp" flush= "true"/> It always checks for changes in the included files, is suitable for containing dynamic pages, and can take parameters.

Static include is implemented with the include pseudo-code and will not check for changes in the included files, and is suitable for containing static pages <%@ include file= "included.htm"%>

The following is a very detailed explanation of how JSP dynamic include and static include are used and how they differ:

We all know that there are two types of include in JSP, namely

<%@ include file= ""%>

<jsp:include page= "" flush= "true"/>

The former is the instruction element, the latter is the behavior element. Where are they going to be used? What is the difference between using them and what they are? This should be a problem that many people see. Let's take a look at it.

Usually when some parts of all the pages in the application (such as the title, footer, and navigation bar) are the same, we can consider using include. Specifically, when to use <%@ include file= ""%> and when to use <jsp:include Page= "" flush= "true"/>. This form. The first thing to understand is the difference between them. Only by understanding the differences in their usage can we understand when to use and how to choose.

<%@ include file= ""%>,jsp the include directive element reads the contents of the specified page. and merge the content with the original page. (This process is done during the translation phase: that is, the JSP is transformed into a servlet.)

Here is a description of the translation phase: We know that the JSP page cannot be passed intact to the browser, all JSP elements must first be processed by the server. 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 (called a JSP page implementation class?). JSP Page Implementation Class), and compile 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. Here's an example.

Server with Tomcat, the JSP file that introduces the page is called test.jsp. The page being introduced is called date.jsp. This JSP file is a JSP code about the time, the current context root is set to test

Source files for ======date.jsp =====//

<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>


Java.util.Date date=new java.util.Date ();

String DATE_CN = "";

String datestr = "";

Switch (Date.getday ())


Case 0:DATE_CN = "Day"; Break

Case 1:DATE_CN = "one"; Break

Case 2:DATE_CN = "two"; Break

Case 3:DATE_CN = "three"; Break

Case 4:DATE_CN = "four"; Break

Case 5:DATE_CN = "five"; Break

Case 6:DATE_CN = "six"; Break


Datestr = (1900+date.getyear ()) + "year" + (Date.getmonth () +1) + "month" + date.getdate () + "Day (week" + DATE_CN + ")";


document.write ("<%=dateStr%>");

====== The following is the source file for test.jsp =============//

<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>

Two ways to use <title>include </title>

<jsp:include page= "date.jsp" flush= "true"/>

<%[email protected] include file= "date.jsp"%-->

We are here to introduce date.jsp this file in two different forms of include.



There are two ways to use include in JSPs. Stay tuned.



In the test.jsp file, we only output one line of text "about the two usages of include in JSPs." Now let's start with <%@ include file= "date.jsp"%> This form introduces date.jsp to this file. Do you think there's going to be a problem? error message appears:

HTTP Status 500?

Org.apache.jasper.JasperException:/date.jsp (0,0) Page Directive:can ' t has multiple occurrences of contentType

Here's a bunch of bugs, but we'll just have to look at what's going on here. The status code is an HTTP 500 server internal error. Look at the following tips. You cannot specify more than one contenttype in the Date.jsp page.

That's why it's here. Because in the translation phase, the code of the date.jsp file is added to the test.jsp page intact to synthesize a file. The file will be the same after the synthesis:

<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>

This code. The solution is to delete this sentence from the date.jsp file. Refresh and then request test.jsp page

Request test.jsp is displayed on the page as follows

December 10, 2003 13:12:40

There are two ways to use include in JSPs. Stay tuned.

We can't find anything yet. Check out the temporary files under Tomcat. Go over there and see if the contents of the date.jsp file have been added to the test.jsp file.

< note. Tomcat here is installed under the e-Packing directory >



In this directory you will see and Test_jsp.class two files.

Here the Java file is the JSP container to convert the JSP into a servlet to get the this file.

The corresponding test_jsp.class this file is compiled the servlet file generated by the class file. Opens the resulting servlet file ( At this point we will find that in test.jsp When the file is converted to a servlet file, it adds some code that is not in the test.jsp page between the

These are the results we get with <%@ include file= "date.jsp"%> this form.

Let's swap with <jsp:include page= "dae.jsp" flush= "true"/>

<%@ include file= "date.jsp"%> replaced <jsp:include page= "dae.jsp" flush= "true"/>, and then request test.jsp.

2003? Ê12?? 10?? 13:30:13

There are two ways to use include in JSPs. Stay tuned.

This will be seen on the page. The date in which we introduced the date.jsp output was garbled in Chinese. What reason? is because the include behavior element is executed during the request processing phase (where the request processing phase is explained). The JSP container is responsible for invoking the JSP page implementation class to process each request and generate an answer, in addition to the above-mentioned conversion of the JSP page into a servlet. This stage is called the request processing phase. The request processing phase executes only the class file.

So when we introduce a page to the include behavior element, we actually simply refer to the Servlet class file that date.jsp this file to be generated after it was converted and compiled. Date.jsp is called when the test.jsp file is run as a separate file. Because there is no character encoding specified in the date.jsp file. So there was garbled. The solution is to re-remove the. date.jsp file.

<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>

This line of statements is added and refreshed again. The page is displayed correctly and is the same as when the include directive is running. Then look at the temporary files under Tomcat and find out. There is a file and a Date_ Jsp.class file. These two files are obtained in the same way as the and Test_jsp.class files. Look at the code for the file at this point. Only a new code is added at this point:

Jspruntimelibrary.include (Request, Response, "date.jsp", out, true);

It does not add the code for the date.jsp file to the test.jsp.

Only the answers generated after the date.jsp page execution are introduced at run time. This means that we can specify any Web resource that can generate an answer, such as a servlet or a JSP page, as long as the types produced by these resources are the same as the content types produced by the JSP pages. The JSP container executes the specified resource through an internal function call. As a result, these introduced resources can help with the original request, so these resources can access all objects within the request scope. And all the original request parameters.

Because these pages have not yet been introduced to the master page when the main pages are requested, you can use a request-time property value on the page property to determine which page to introduce, depending on the runtime. You can also add some request parameters that are read from the page that will be introduced.

<jsp:include page= "<%=pageSelectedAtRuntime%>" flush= "true" >

<jsp:param name= "Fitstparamer" value= "Firstvalue" >

<jsp:param name= "Lastparamer" value= "Lastvalue" >


If you modify the JSP page that was introduced, you can immediately use the latest version of the page, because the page is being introduced in exactly the same way as a JSP page that is called directly by the browser. That is, the container detects the changes in the page and automatically enters the translation phase to get the latest version of the page.

(Note that the include behavior element, like other JSP elements, ends with "/" when there is no body.) Just like the following.

<jsp:include page= "<%=pageSelectedAtRuntime%>" flush= "true"/>)

The following are the differences between the include two usages

There are two main aspects of the difference;

One: The execution time:

<%@ include file= "relativeuri"%> is executed during the translation phase

<jsp:include page= "Relativeuri" flush= "true"/> is executed 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 execution page or servlet.

In addition, the file and page properties are interpreted as a relative URI in both usages. If it starts with a slash, it is an environment-dependent path. It is interpreted according to the prefix of the URI assigned to the application, if it does not start with a slash, then it is the page-dependent path. This is explained by the path to the page where the file was introduced. For more information about how URLs are interpreted, please refer to the relevant materials or books.

The difference between dynamic include and static include in JSP

Related Article

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