Differences between include directives and include behaviors in JSP _jsp programming

Source: Internet
Author: User
Tags flush

<%@ include file= ""%>

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

The former is the instruction element and the latter is the behavioral element. Where exactly will they be used? How to use and what is the difference between them? This should be a problem that many people will think of when they see it. Let's take a look below.

Typically, when parts of all pages in an application, such as headers, footers, and navigation bars, are the same, we can consider using include. When to use the <%@ include file= ""%&gt, and when to use < Jsp:include page= "" flush= "true"/&GT; 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.

The include directive elements of the <%@ include file= ""%>,jsp read 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 JSP pages 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. JSP containers are typically implemented in the form of a servlet, which is configured to handle all requests for JSP pages.

The JSP container is responsible for translating the JSP page into a servlet, called a JSP page implementation class? JSP Page Implementation Class), and compile this servlet. These two steps constitute the translation phase.

From this we will know: The JSP page is the content of the page specified by the Include instruction element (that is, the code snippet) is added to the JSP page that introduces it, and the JSP container converts it into a servlet after the synthesis of a file. You can see that this will result in a temporary class file and a Java file. Here's an example.

Server with Tomcat, the introduction of the page JSP file called test.jsp. The page being introduced is called date.jsp. This JSP file holds a JSP code about the time, and the current context root is set to test

======date.jsp 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 = "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 file 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"%-->
Here we introduce the date.jsp file with two different forms of include.
<body>
<table><tr><td>
Two uses for include in the JSP. Please be concerned.
</td></tr></table>
</body>

In the test.jsp file, we output only one line of text "two usages of the include in the JSP." Stay tuned. "Now let's introduce the date.jsp file in this form first." Do you think there's going to 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 problem 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. Because in the translation phase, the code for the date.jsp file is added to the test.jsp page intact to synthesize a file. The resultant file will be the same:

<%@ 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

The request test.jsp is displayed on the page as follows

August 12, 2007 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 file have been added to the test.jsp file.

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

The Java file here is the Test_jsp.java file that the JSP container translates into a servlet.

Corresponding to the Test_jsp.class this file is compiled Test_jsp.java the servlet file generated by the class file. Open the resulting servlet file (Test_jsp.java). At this point, we will find that when the test.jsp file is converted into a servlet file, the output of < Haed > is added between the code is not test.jsp page, the new content is date.jsp inside the code: What's new or whether it's actually added to the new content please test yourself to see it. No more details here.

These are the results that we use with the <%@ include file= "date.jsp"%> this form.

Here we swap < Jsp:include page= "dae.jsp" flush= "true"/> which will

<%@ include file= "date.jsp"%> replaced by < Jsp:include page= "dae.jsp" flush= "true"/&gt, and then request test.jsp.

2007? Ê8?? 12?? 13:30:13

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

You will see it on the page. The date in which we introduced the date.jsp output is garbled in Chinese. What reason? Because the include behavior element is executed during the request processing phase (this is where the request processing phase is described). The JSP container is responsible for invoking the JSP page implementation class to process each request and generate an answer in addition to the above mentioned responsibility for translating the JSP page into a servlet. This phase is called the request processing phase. The request processing phase executes only the class file.

So when we introduce the include behavior element into the page, we actually simply refer to the Servlet class file that date.jsp the file that was converted and compiled. Date.jsp is invoked as a separate file before it is run by the test.jsp file. Because there is no character encoding specified in the date.jsp file. The solution is to get rid of it in the date.jsp file.

This line of statements is added after the refresh is rerun. The page is displayed correctly and is the same as if the include instruction is running normally. And then look at the temporary files under Tomcat. There is a Date_jsp.java file and a Date_ Jsp.class files. These two documents come in the same way as Test_jsp.java and Test_jsp.class files. And then look at this. The code for the Test_jsp.java file is found. This time only adds a new code:

Jspruntimelibrary.include (Request, Response, "date.jsp", out, true);

It does not add the code of the date.jsp file to the test.jsp.

Only the response generated by the Date.jsp page execution is introduced at run time. This means that we can specify any Web resource that can generate an answer, such as a servlet or a JSP page, as long as those resources produce the same type of content that the JSP page produces. The JSP container executes the specified resource through an internal function call. Therefore, these introduced resources can help to process the original request, so these resources can access all the objects within the request scope. And all the original request parameters.

Because the pages have not been introduced into the main page when the primary page is requested, you can use a request-time property value on the page property to determine which page to introduce based on the run-time situation. You can also add request parameters that will be read by the page being introduced.

<jsp:include page= "<%=pageSelectedAtRuntime%>" flush= "true" >
<jsp:param name= "Fitstparamer" value= "Firstvalue" >
<jsp:param name= "Lastparamer" value= "Lastvalue" >
</jsp:include>

If you modify the introduced JSP page, then you can use the latest version of the page immediately, because the approach to the introduced page is exactly the same way you would treat a JSP page that was called directly by the browser. The container detects the changes in the page and automatically enters the translation phase to get the latest version of the page.

(Note that the include behavior element, like other elements of the JSP, ends with "/" when there is no action body.) just like the following.

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

The following are the differences between the include two usages

There are mainly two different aspects;

1. Execution Time:

<%@ include file= "Relativeuri"%> is performed during the translation phase

< Jsp:include page= "Relativeuri" flush= "true"/> executed at the request processing stage.

2. The introduction of different content:

<%@ include file= "Relativeuri"%>

Introduce static text (html,jsp) to integrate the JSP page before it is converted into a servlet.

< Jsp:include page= "Relativeuri" flush= "true"/> Introduces the response text generated by the execution page or servlet.

In addition, both the file and Page properties are interpreted as a relative URI in both usages. If it starts with a slash, it is an environment-related path. will be interpreted according to the prefix of the URI assigned to the application, if it is not preceded by a slash, then the page-related path. Explain the path to the page where the file was introduced. For more information on how URLs are interpreted please refer to the relevant materials or books.
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.