Two usages of include in a JSP __jsp

Source: Internet
Author: User
Tags flush tomcat

We all know that there are two kinds of situations in JSP including the

<%@ include file= ""%>

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

The former is the instruction element and the latter is the action element. Exactly where they will be used. How to use and what difference they make. This should be a topic that many people will think of when they see it. Let's take a look below.

Usually when you use some parts of a program's entire page (for example, the title, footer, and navigation bar) to be identical, we can consider using include. Specify when to use <%@ include file= ""%>

<jsp:include page= "" flush= "true"/> this situation. The first thing to know is the difference between them. It is only by understanding the differences in their usage that you know when to use and how to choose.

<%@ include file= ""%>

The include instruction element of the JSP reads the contents of the specified page. and combine the content with the original page. (This process is in the translation phase: that is, the JSP is translated into a servlet stage.)

Here is a description of the translation phase: We know that the JSP page can not be passed intact to the browser, all JSP elements must first be processed by the server. This is done by conveying the JSP page into a servlet and then executing the servlet. The server needs a JSP container to process JSP pages. The JSP container is usually implemented as a servlet, and this servlet is configured to handle all requests for JSP pages.

The JSP container values the JSP page into a servlet (called the JSP page implementation Class-JSP page implementation Class) and compiles the servlet. These two steps constitute the translation phase.

As a result, we will know that the JSP page is the actual content of the page specified by the Include directive element (that is, the code snippet) participates in the JSP page that introduces it, the synthesis of a document is converted into a servlet by the JSP container. You can see that this will produce a temporary class document and a Java document. Here's an example.

The server uses Tomcat, the JSP document that introduces the page is called test.jsp. The page being introduced is called date.jsp. This JSP document holds a JSP code about the time, and the current context root is set to test

======date.jsp Source Document =====//

<%@ 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 = "one"; Break

Case 2:DATE_CN = "two"; Break

Case 3:DATE_CN = "three"; Break

Case 4:DATE_CN = "four"; Break

Case 5:DATE_CN = "five"; Break

Case 6:DATE_CN = "six"; Break

}

Datestr = (1900+date.getyear ()) + "year" + (Date.getmonth () +1) + "month" + date.getdate () + "Day (week" + DATE_CN + ")";

%>

document.write ("<%=dateStr%>");

= = = Below is the source document for Test.jsp =============//

<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>

Two usages of <title>include </title>

<jsp:include page= "date.jsp" flush= "true"/>

<%--@ include file= "date.jsp"%-->

We are here to introduce the date.jsp document with the two kinds of compassion that include.

<body>

<table><tr><td>

Two uses for include in the JSP. Please be concerned.

</td></tr></table>

</body>

In the test.jsp document, we output only one line of text "two usages of the include in the JSP." Stay tuned. "Now let's use the <%@ include file=" date.jsp "%> this situation to introduce date.jsp this document. Do you think there will be a problem? An error message appears:

HTTP Status 500–

Org.apache.jasper.JasperException:/date.jsp (0,0) Page Directive:can ' t have multiple occurrences of contentType

Here's a bunch of bugs, but we'll just have to look here to see what the target is. The status code is an HTTP 500 server internal error. Look at the tips below. You cannot specify more than one contenttype in a date.jsp page.

The reason is here. is because in the translation phase, the code for the date.jsp document is taken to the test.jsp page to synthesize a document. The resultant document will be identical:

<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>

This code. The solution is to delete the sentence in the date.jsp document. Refresh and then request test.jsp page

Request test.jsp on the page to reveal the following

December 10, 2003 13:12:40

Two uses for include in the JSP. Please be concerned.

We can't find anything yet. Let's check out the temporary files under Tomcat. Go there and see if the contents of the date.jsp document are already in the test.jsp document.

< note. Here tomcat is installed in the e-packing directory >

Directory

E:/tomcat/work/standalone/localhost/test.

In this directory you will see

Test_jsp.java and Test_jsp.class two documents.

The Java document here is the Test_jsp.java document that the JSP container has transformed JSP into a servlet.

The corresponding Test_jsp.class is the document that compiles the class document produced by Test_jsp.java this servlet document. Open the resulting servlet document (Test_jsp.java). At this point we will find that

When a test.jsp document is converted into a servlet document, some code in the

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.