Use of static and dynamic content on JSP pages.

Source: Internet
Author: User

Use of static and dynamic content on JSP pages.

There are two types of JSP: static include: <% @ include file = "included page" %> and dynamic include: <jsp: include page = "included page" flush = "true">. The following is an example to illustrate how to use inclusion.

Instance:

Project File tree:

Content in the header. jsp file:

Content in the content. jsp file

<table border="1"> <tr>  <td>col1</td>  <td>col2</td> </tr> <tr>  <td>col1</td>  <td>col2</td> </tr></table>

Content in the footer. jsp file:

Static include:

Content in the index. jsp file:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%@ include file="/header.jsp" %><%@ include file="/content.jsp" %><%@ include file="/footer.jsp" %>

Result displayed on the page:

Compiled java files:

Dynamic inclusion

Content in the index. jsp file:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><jsp:include page="/header.jsp"></jsp:include><jsp:include page="/content.jsp"></jsp:include><jsp:include page="/footer.jsp"></jsp:include>

Result displayed on the page:

Compiled java files:

Description

In this example, static inclusion is the same as dynamic inclusion, but the implementation process is different. Obviously, the number of compiled java files is different.

Differences and connections between static and dynamic inclusion

Note: The jsp file that contains other files is called the main file, for example, the index. jsp file in the preceding section. The contained file is called a contained file, such as the header. jsp file in the preceding section.

<% @ Include file = "%> is a command element. <Jsp: include page = ""/> indicates that the number of behavior elements ultimately compiled into java files varies. (As shown in the preceding example)

Static inclusion: when converting to a java file, the content of the contained file is "copied" to the main file and then compiled as a whole. Finally compiled into a java file.

Dynamic inclusion refers to the conversion and compilation of jsp files separately. Finally, it is programmed into multiple java files.

Different execution times

Static inclusion occurs in the JSP ----> java file stage.

Dynamic inclusion occurs in the stage of executing the class file. Dynamic addition.
Static inclusion cannot have the same variables in two files. Dynamic inclusion is allowed.
Static inclusion is equivalent to directly copying the content of the contained file to the main file. If the same variable exists, overwriting and other problems will occur, leading to file errors. Dynamic inclusion is equivalent to calling different JSPs. variables are in different spaces and will not be overwritten.
Whether it is dynamic or static, the request object is the same. That is, the same request object.
Static inclusion is finally compiled into a java file, and a request object is easy to understand. Why is a request object used when dynamic inclusion is finally compiled into multiple jsp files? In fact, the jsp combination process is a request forwarding process, and naturally the same request object is used.

Static and Dynamic inclusion

To sum up, the contained page uses static content for static content, and dynamic content for dynamic pages. (Of course, it is not absolute, but there is no error in this case .)

 Differences between static and dynamic jsp content:

There are two types of jsp, static including <% @ include file = "xxx. jsp "%> and dynamic include <jsp: include page =" xxx. jsp ">, the difference between them is described below

1. <% @ include file = "xxx. jsp "%> is the Compilation instruction in jsp. The file inclusion occurs during the jsp-to-servlet conversion period, and <jsp: include page =" xxx. jsp "> is an action instruction in jsp. Its File Inclusion occurs in the compilation period, that is, the period when java files are compiled into class files.

2. When static inclusion is used, only one class file is generated. When dynamic inclusion is used, multiple class files are generated.

3. Use static inclusion. The request object that contains the page and the page to be included is the same object, because static inclusion only copies the content of the contained page to the contained page; dynamic inclusion of pages and contained pages are not the same page. The range of parameters that can be obtained from the request object of the contained page is relatively large, not only can the parameters passed to the include page be obtained, but also the parameters passed down to the include page can be obtained.

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.