JSP static Import and dynamic import

Source: Internet
Author: User

jsp static import (JSP Directive markup include)

When the 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. JSP directives mark the JSP page translation to provide information about the entire page.

The include directive is used to insert a file statically on a JSP page, and the inserted file can be a JSP page, an HTML page, a text file, or a piece of Java code. JSP pages that use the include directive, when converted to a Java file, insert the inserted file in the current JSP page where the instruction is inserted into a new JSP page, and then the JSP engine translates the new JSP page into a Java file. Therefore, it is important to ensure that the new JSP page formed after inserting the file conforms to the JSP syntax and logic rules. The include directives are used in the following format:

<%@ page include file= "URL of File"%>
The Insert file can use absolute and relative paths, typically using relative paths. The relative path is relative to the directory where the current page is located, for example, the current page is located in the Web service root directory, the inserted file is in the root directory of the text file, the file name is Head.txt, and the relative path is formatted as follows:
<%@ page include file= "Text/head.txt"%>
When the inserted file is modified, the JSP engine will re-merge the current JSP page and the modified embedded file into a new JSP page, then translate and compile the new Java byte file for the client to access.

Use the include directive to enable code reuse and improve the efficiency of code usage. For example, the page generally needs a title bar, navigation bar, footer, etc., the user can write the title bar and so on as a separate file, each JSP page can be in the appropriate location with include directives inserted in the title bar, navigation bar, footer and other files, which greatly improve the code reuse and efficiency, make the code more convenient to modify.

It is worth noting that the inserted file is a text file should be noted in the beginning of the file to indicate the relevant information of the file, so that the convenience of JSP pages and embedded files to create a new JSP page does not appear garbled and so on. For 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, and if the contained page is a JSP page, it is processed before the result is included, and if it contains a non-*.jsp file, it simply contains the contents of the file statically. The containing page program is independent of each other and is complementary to the included page program. The general use format of the jsp:include tag is:

<jsp:include page= "url of file"/> or <jsp:include page= "URL of File" >       <jsp:param child tag/></jsp:include >



The difference between a JSP dynamic import and a static import:

1, processing time and different ways: include directive tag inserted files are merged together in the page translation, the inclusion of the file and the current page combination of the new page must conform to the JSP syntax and logic rules, because it is compiled in advance, so the execution speed, and JSP: Include action tags are included file syntax and logic is independent of the current page, is compiled by the JSP engine, the current page is executed and then the results of the contained files are sent to the client, because it is the execution of the page when processing the include file, so the execution is slow, but can take advantage of the Param child tag passed parameters, More flexible to use.

2, the applicable situation is different: static import is suitable for importing resources such as HTML files that do not change frequently, and dynamic import is suitable for a constantly changing resource such as JSP files.




JSP static Import and dynamic import

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.