Differences between include commands and include actions in JSP (1)

Source: Internet
Author: User

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

<% @ Include file = "%>

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

The former is the directive element, and the latter is the action element. Where will they be used? How to use them? This should be a problem that many people will think. Let's take a look.

Generally, when all the parts of a page, such as the title, footer, and navigation bar, are the same, we can consider using include. When to use <% @ include file = "" %> and <jsp: include page = "" flush = "true"/>. 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 = "%>. The include Directive element of jsp reads the content of the specified page. And integrate the content with the original page. This process is carried out in the translation phase, that is, the jsp is converted into a servlet.

The translation phase is explained here: we know that the jsp page cannot be transmitted to the browser as it is, and all jsp elements must be processed by the server first. This is done by converting the jsp page into a servlet and executing the servlet. The server needs a jsp Container to process the jsp page. Jsp containers are usually implemented in the form of servlets. this servlet is configured to process all requests to the jsp page.

Is the Jsp Container responsible for converting jsp pages into servlet called jsp page implementation classes? JSP Page implementation class), and compile this servlet. These two steps constitute the translation phase.

From this we will know that the jsp page adds the actual content of the page specified by the include command element, that is, the code segment, to the jsp page that is introduced to it, after a file is merged, the jsp Container converts it into a servlet. A temporary class file and a java file are generated. The following is an example.

Tomcat is used on the server. The jsp file of the introduced page is called test. jsp. The introduced page is called date. jsp. This jsp file stores a jsp code related to time. The current context root is set to test

Source file of 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 = ""; break; case 1: date_cn = "1"; break; case 2: date_cn = "2"; break; case 3: date_cn = "3"; break; case 4: date_cn = "4"; break; case 5: date_cn = "5"; break; case 6: date_cn = "6"; break;} dateStr = (1900 + date. getYear () + "year" + (date. getMonth () + 1) + "month" + date.
GetDate () + "Day (Week" + date_cn + ")"; %> document. write ("<% = dateStr %> ");

The following are the source files of test. jsp:

<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
<Html>
<Head>
<Title> two methods of include </title>
<Jsp: include page = "date. Jsp "flush =" true "/>
<% -- @ Include file = "date. Jsp "% -->
// Here we use two different types of include to introduce date. Jsp file.
<Head>
<Body>
<Table> <tr> <td>
Two Methods of include in jsp are described. Stay tuned.
</Td> </tr> </table>
</Body>
</Html>

In the test. jsp file, we output only one line of text "Two include usage in jsp. Stay tuned .", Now let's introduce the file date. jsp in the form of <% @ include file = "date. jsp" %>. Do you want to see any problems? The following error message is displayed:

HTTP Status 500 ?
org.apache.jasper.JasperException: /date.jsp(0,0) Page directive:
can't have multiple occurrences of contentType


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.