JSP static import and dynamic import, jsp static Import

Source: Internet
Author: User
Tags file url

JSP static import and dynamic import, jsp static Import
JSP static import (JSP command mark include)

When a JSP page is requested for the first time, it is translated into a Servlet Java file by the JSP engine and then compiled into a bytecode file for execution.The JSP command is marked as JSP page translation to provide information about the entire page.

The include command is used to insert a static file into a JSP page. The inserted file can be a JSP page, HTML page, text file, or a Java code. When the JSP page using the include command is converted to a Java file, the inserted file is inserted at the location where the command is generated on the current JSP page, then, the JSP Engine translates the new JSP page into a Java file. Therefore, the new JSP page formed after the file is inserted must comply with the JSP syntax and logic rules. The format of the include command is:

<% @ Page include file = "file URL" %>
The absolute path and relative path can be used to insert a file. Generally, relative paths are used. Relative, the relative path format is as follows:
<%@ page include file="text/head.txt" %>
After the inserted file is modified, the JSP Engine merges the current JSP page and the modified embedded file into a new JSP page, then translate and compile a new Java byte file for your access.

The include command can be used to reuse code and improve code efficiency. For example, a page generally requires a title bar, navigation bar, footer, etc. You can write the title bar as a separate file, each JSP page can insert files such as the title bar, navigation bar, and footer with the include command at an appropriate location, which greatly improves code reuse and efficiency and makes code easier to modify.

It is worth noting that when the inserted file is a text file, you must specify the relevant information of the file in the beginning of the file, in this way, it is convenient to combine JSP pages and embedded files to generate a new JSP page without garbled characters. Example (head.txt ):

<% @ Page contentType = "text/html; charset = GB2312" %> 



JSP dynamic import (jsp: include action tag)

The include action tag is used to dynamically include a file in a JSP page. If the included page is a JSP page, it is processed before the result is included. If the included page is not *. jsp file, the file content is static. The included page program and the included page program are independent of each other and affect each other. Jsp: include tags are generally used in the following format:

<Jsp: include page = "File URL"/> or <jsp: include page = "File URL"> <jsp: param sub-tag/> </jsp: include>



Differences between dynamic JSP import and static import:

1. The processing time and method are different: the include command marks the inserted files as merged during page translation, the new page composed of the contained file and the current page must comply with the JSP syntax and logic rules. Because it is compiled in advance, the execution speed is fast. jsp: the include Action mark is the syntax and logic of the included file independent of the current page. It is compiled by the JSP Engine separately. When the current page is executed, the running result of the included file is sent to the client, because it processes include files during page execution, the execution speed is slow. However, you can use the param sub-mark to pass parameters, making it more flexible.

2. Different application scenarios: static imports are suitable for importing resources that do not change frequently, such as html files, while dynamic imports are suitable for a resource that often changes, such as JSP files.





Dynamic INCLUDE and static INCLUDE in JSP are different

When using dynamic INCLUDE, the page to be included (sorted. jsp in your example) will be parsed and displayed together with the home page;
My task is to include and display you.

Jsp: include: Compile the embedded ded. jsp file first, then include the compiled file, and then include
@ Include is to first include the file, then the unified compilation first contains, and then the compilation

Personal Understanding, hope to make you understand

What is the difference between dynamic INCLUDE and static INCLUDE in JSP?

Dynamic INCLUDE is implemented using jsp: include action. It always checks changes in the contained files. It is suitable for inclusion of dynamic pages and can contain parameters.
Static INCLUDE is implemented by using the include pseudo code and will not check the changes in the contained files. It is applicable to INCLUDE static pages.

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.