Differences between include commands and include actions in JSP

Source: Internet
Author: User

We all know that there are two types of include in jsp (the preferred choice for SUN Enterprise Applications ):
<% @ Include file = "%>
<Jsp (preferred for SUN Enterprise Applications): include page = "flush =" true "/> </P>


The former is a command element, and the latter is a behavior 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. </P>


Generally, when all parts of a page (such as the title, footer, and navigation bar) in an application are the same, we can consider using include. When to use <% @ include file = "" %> and <jsp (preferred for SUN Enterprise Applications ): 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 = "%>, the include command element of jsp (preferred for SUN Enterprise Applications) reads the content of the specified page. And integrate the content with the original page. (This process is in the translation phase: jsp (the first choice for SUN Enterprise Applications) is converted into servlet. </P>


Here we will explain the translation phase: We know that jsp (the preferred choice for SUN Enterprise Applications) pages cannot be transmitted to browsers as they are, and all jsp (the preferred choice for SUN Enterprise applications) all elements must be processed by the server first. This is done by converting the jsp (preferred for SUN Enterprise Applications) page into a servlet and executing this servlet. The server needs a jsp (preferred for SUN Enterprise Applications) container to process the jsp (preferred for SUN Enterprise Applications) page. Jsp (preferred for SUN Enterprise Applications) containers are usually implemented in the form of servlets. this servlet is configured to process all requests on the jsp (preferred for SUN Enterprise Applications) page. </P>


Jsp (preferred for SUN Enterprise Applications) containers are responsible for converting jsp (preferred for SUN Enterprise Applications) pages into servlet (called jsp (preferred for SUN Enterprise Applications) page implementation class? Jsp (preferred for SUN Enterprise Applications) Page implementation class) and compile this servlet. These two steps constitute the translation phase. </P>


As a result, we will know that the jsp (first choice for SUN Enterprise Applications) page refers to the actual content (that is, the code segment) of the page specified by the include command element) add it to the jsp (preferred for SUN Enterprise Applications) page to merge a file and convert it into a servlet by the jsp (preferred for SUN Enterprise Applications) container. A temporary class file and a java file are generated. The following is an example. </P>


The server uses tomcat (a very useful JSP running platform). The jsp file that introduces the page (the first choice for SUN Enterprise Applications) is named test. jsp (the first choice for SUN Enterprise applications ). The introduced page is called date. jsp (the first choice for SUN Enterprise applications ). this jsp (preferred for SUN Enterprise Applications) file stores a time-related jsp (preferred for SUN Enterprise Applications) code. The current context root is set to test
// ======= Date. jsp (preferred for SUN Enterprise Applications) source file ====== //
<% @ 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 = "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 is the source file of test. jsp (preferred for SUN Enterprise Applications) ======================== //
<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
<Html>
<Head>
<Title> two methods of include </title>
<Jsp (preferred for SUN Enterprise Applications): include page = "date. jsp (preferred for SUN Enterprise Applications)" flush = "true"/>
<% -- @ Include file = "date. jsp (preferred for SUN Enterprise Applications)" % -->
// Here we use two different types of include to introduce the file date. jsp (preferred for SUN Enterprise applications.
<Head>
<Body>
<Table> <tr> <td>
For more information about the two Use Cases of include in jsp (the preferred choice for SUN Enterprise applications), see.
</Td> </tr> </table>
</Body>
</Html> </P>


In test. in the jsp (preferred for SUN Enterprise Applications) file, we only output a line of text "Two Use Cases of include in jsp (preferred for SUN Enterprise applications. stay tuned. ", Now let's first introduce the file date. jsp (preferred for SUN Enterprise Applications) in the form of <% @ include file = "date. jsp (preferred for SUN Enterprise Applications)" %>. Do you want to see any problems? The following error message is displayed:
HTTP Status 500?
Org. apache (the most popular WEB server platform on Unix ). jasper. jasperException:/date. jsp (preferred for SUN Enterprise Applications) (0, 0) Page directive: cant have multiple occurrences of contentType
There are a bunch of errors below, but we only need to check the problem here. The status code is an internal error of the http 500 server. Let's look at the following prompt. You cannot specify multiple contenttypes on the date. jsp (SUN Enterprise Application preferred) page. </P>

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