JSP include instructions and actions and differences in the detailed

Source: Internet
Author: User
Tags flush tomcat

Let's take a look at grammar and comparison.





Include_ <%@ page language= "java"  import= "java.util.*"  contenttype= "text/html; "
Charset=utf-8 "%> <% String path = request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport ()
+path+ "/"; %> <!
doctype html public  "-//w3c//dtd html 4.01 transitional//en" > 
//data.jsp <%@ page language= "java"  import= "java.util.*" ,  java.text.* " contenttype=" Text/html; charset=utf-8 "%> <%     
Request.setcharacterencoding ("Utf-8");
    date d = new date ();
    simpledateformat sdf = new simpledateformat ("YYYY year mm month DD Day");
    out.println (Sdf.format (d)); %> 
include_action.jsp <%@ page language= "java"  import= "java.util.*"  contenttype= "text/"
Html; charset=utf-8 "%> <% String path = request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport ()
+path+ "/"; %> <!
doctype html public  "-//w3c//dtd html 4.01 transitional//en" > 





JSP include instructions and include action to distinguish between the detailed


We all know that there are two forms of include in JSP, namely

<%@ include file= ""%>

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

The former is the instruction element, the latter is the action 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. Specifically, when using <%@ include file= ""%>, when to use the Jsp: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.

The include directive element of the <%@ include file= ""%>,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 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

Source files for 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 = "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%>");


The following are the source files for test.jsp:

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

<title> two uses of include </title>
<jsp:include page= "date. JSP ' flush= ' true '/>
<%--@ include file= "date. JSP "%-->
Here we introduce date in two different forms of include. JSP this file.
<body>
<table> <tr> <td>
Two uses for include in the JSP. Please pay attention.
</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 the form of <%@ include file=" date.jsp "%>. 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 file, refresh and then request the test.jsp page.

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

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

The following 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, there are some code in the output
These are the results that we use <%@ include file= "date.jsp"%> this form.

Let's switch to <jsp:include page= "dae.jsp" flush= "true"/> to replace <%@ include file= "date.jsp"%> with <jsp:include page= "dae.jsp" flush = "true"/>, and then request test.jsp.

2003? Ê12?? 10?? 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 have introduced the date.jsp output is garbled in Chinese. What reason? Because the include behavior element is executed during the request processing phase (here's a description of the request processing phase, In addition to the above, the JSP container is responsible for translating JSP pages into Servlet, and is also responsible for invoking the JSP page implementation class to process each request and generate an answer. 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.

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



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: The Include action element, like other elements of the JSP, ends with "/" when there is no body. Just like the following. <jsp:include page= "<%=pageSelectedAtRuntime%>" flush= "true"/>

The following are the differences between the include two usages, mainly in two different ways:

On execution time:

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

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

Introduction of different content:

The <%@ include file= "Relativeuri"%> introduces static text (html,jsp) and brings it together before the JSP page is converted to 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.

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.