Two JSP import methods

Source: Internet
Author: User
Note: I have excerpted it before. Now I want to check it out and stick it to it.

 We all know thatJSPMediumIncludeThere are two forms,They are
<% @ Include file = "%>
<JSP: Include page = "flush =" true "/>

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.

generally, when all parts of the page (such as the title, footer, and navigation bar) in the Program are the same, we can consider using include . When to use <% @ include file = "" %> , when to use . . 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 = "" %>, JSP command element of "> reads the content of the specified page. And integrate the content with the original page. (This process is in the translation phase : that is, JSP is converted to the servlet stage.

here we will explain the translation phase : as we know, JSP pages cannot be transmitted to browsers intact, all JSP elements must be server . This is by converting the JSP page into servlet , and then execute this servlet . server A JSP container is required to process JSP page. JSP containers generally use servlet is configured, it can process all requests to the JSP page.

JSPThe container is responsibleJSPConvert pagesServlet(CalledJSPPage implementation class? JSP page implementation class),And compile thisServlet. These two steps constitute the translation phase..

As a result, we will know:JSPThe page isIncludeThe actual content of the page specified by the directive element (that isCodeSegment) added toJSPPage,After a file is mergedJSPContainer converts itServlet. We can see that a temporaryClassFile andJavaFile. The following is an example.

Server Tomcat , Introduce JSP File Name Test. jsp . The introduced page is called Date. jsp. This JSP The file is stored at a specific time. JSP Code , The current context root is Test
// ======= Date. jsp Source File ====== //
<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" %>
<%
Java. util. Date = new java. util. Date ();
String date_cn = "";
String datestr = "";
Switch (date. getday ())
{
Case 0: date_cn =" Day "; Break;
Case 1: date_cn =" I "; Break;
Case 2: date_cn =" II "; Break;
Case 3: date_cn =" 3. "; Break;
Case 4: date_cn =" Thu "; Break;
Case 5: date_cn ="V. "; Break;
Case 6: date_cn =" Sat. "; Break;
}
Datestr = (1900 + date. getyear () +" Year "+ (Date. getmonth () + 1) +" Month "+ Date. getdate () +" Day ( Week "+ Date_cn + ")";
%>
Document. Write ("<% = datestr %> ");
// ====== Below is Test. jspSource File ==================== //
<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head>
<Title> include Two usage </Title>
<JSP: Include page = "date. jsp" Flush = "true"/>
<% -- @ Include file = "date. jsp" % -->
// We use Include Two different forms Date. jsp This file .
<Head>
<Body>
<Table> <tr> <TD>
Related JSP Medium Include Two usage . Stay tuned.
</TD> </tr> </table>
</Body>
</Html>

in test. in the JSP file, we output only one line of text " in JSP include Stay tuned. ", now let's use <% @ include file =" date. JSP "%> introduced in this form date. JSP file. Do you want to see any problems? The following error occurs:
HTTP status 500?
Org. apache. jasper. jasperexception:/date. JSP () page directive: Can't have multiple occurrences of contenttype
there are also a bunch of errors, but we only need to check the problem here. The status code is HTTP 500 internal server error. Let's look at the following prompt. On the date. jsp page, you cannot specify multiple contenttype.

The reason is here. Because in the translation phase, Date. jspThe file code is added toTest. jspPage to synthesize a file. The merged file will be the same:
<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" %>
This code. The solution isDate. jspDelete this sentence from the file. Request again after refreshingTest. jspPage

 

RequestTest. jspThe following figure is displayed on the page:
2003Year12Month10Day13:12:40
RelatedJSPMediumIncludeTwo usage.Stay tuned.

At this time, we cannot find anything. Check againTomcat. Go there to seeDate. jspWhether the file content has been addedTest. jspFile.
<Note.HereTomcatInstallEUnder the root directory>
Directory
E: "Tomcat" work "standalone" localhost "test.
In this directory, you will see
Test_jsp.javaAndTest_jsp.classTwo files.

HereJavaFile isJSPContainerJSPConvertedServletAndTest_jsp.javaThis file.

corresponding test_jsp.class this file is compiled test_jsp.java This servlet the class file generated by the file. Open the generated servlet file ( test_jsp.java ). At this point, we will find that in test. when JSP files are converted to servlet files, add test to the output . JSP code in the page , : check the added content or whether the new content is added. . I will not detail it here .

we use <% @ include file = "date. JSP "%> result in this form .
next we use it again that is,
<% @ include file =" date. JSP "%> changed to , Find test. jsp.
2003? Listen 12 ?? 10 ?? 13:30:13
related JSP in . Stay tuned.

At this time, you will see.We introduceDate. jspGarbled characters appear in the output date..Why??BecauseIncludeThe behavior element is executed in the request processing stage.(The request processing stage should be described here. JspIn addition toJSPConvert pagesServletExternal,Also responsible for callingJSPPage implementation class to process each request and generate a response.This stage is called the request processing stage..The request processing stage only executes files.).

Therefore Include When a behavior element is introduced to a page , Actually, it just references Date. jsp This file is converted and compiled. Servlet Class File . Both , Date. jsp Is executed only after being executed as a separate file. Test. jsp File runtime call . Because Date. jsp No character encoding is specified in the file. . So garbled . The solution is Date. jsp File to remove
<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" %>
Refresh and run this line of statements . The page is displayed correctly. , And follow-up Include The command runs normally. . Check again Tomcat Under the temporary file will find . At this time, an additional Date_jsp.java File and Date_jsp.class File . The method obtained from these two files is as follows: Test_jsp.java And Test_jsp.class The file is obtained in the same way. . Check again. Test_jsp.javaFile Code will find . At this time, only a new code is added. :
Jspruntimelibrary. Include (request, response, "date. jsp", out, true );

It does notDate. jspAdd the file codeTest. jsp.

only date is introduced during runtime. . JSP response generated after page execution . This means that you can specify any Web resource , ( for example, a servlet or a JSP page ), as long as the types generated by these resources are the same as those generated by JSP pages . JSP the container uses an internal function call to execute the specified resource. . therefore, , these introduced resources can help process original requests , and all original request parameters .

because the homepage is requested , these pages have not been introduced into the home page , so you can use a request attribute value for the page attribute , determines the page to be introduced based on the running condition. . You can also add Request Parameters for reading the introduced pages. .



If the introducedJSPPage,You can use the latest version of the page immediately.,This is because the browser calls the introduced page directly.JSPThe page is in the same way.That is, changes to the container detection page,And automatically enters the translation stage,Get the latest version of the page.

(Note:, IncludeSame behavior elementJSPSame as other elements,When no behavior body exists"/"End.As shown below:.
<JSP: Include page = "<% = pageselectedatruntime %>" flush = "true"/>)

The following are Include Differences between the two methods
There are two main differences: ;
I : Execution time :
<% @ Include file = "relativeuri" %> Is executed in the translation phase
<JSP: Include page = "relativeuri" flush = "true"/> Executed during request processing .
II : Different content introduced :
<% @ Include file = "relativeuri" %>
Introduce static text (HTML, JSP ), In JSPThe page is converted Servlet Previously integrated with it .
<JSP: Include page = "relativeuri" flush = "true"/> Introduce the execution page or Servlet Response text generated .
In addition File And Page Attribute is interpreted as a relative Uri. If it starts with a slash , It is an environment-related path. . Based on Uri Prefix ,If it does not start with a slash , This is the page-related path. , It is explained based on the path of the page where the file is introduced. . More information URL Please refer to relevant materials or books for how to explain .

 

 

 

Carrier:Http://java.chinaitlab.com/ServletJsp/529740_2.html

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.