A detailed explanation of the JSP's static inclusion and dynamic inclusion (i)

Source: Internet
Author: User

There are two ways to include a JSP: static inclusion and dynamic inclusion. Below we will compare the differences between the features of the JSP run (converted to servlet execution).


One, static contains <% @include file= "url"%>

Let's start by writing a normal small project and see how static containment works.

Test One:

top.jsp page:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

footer.jsp page:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

index.jsp page:

<%@ page language= "java"  contenttype= "text/html;  Charset=utf-8 "    pageencoding=" UTF-8 "%><! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd ">

Use static inclusion on the index page, include top and footer two pages, run it now and see how many Java files will be generated in the Temp folder

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/88/38/wKiom1fr1arSanRQAACGc9TPk10382.png-wh_500x0-wm_3 -wmp_4-s_2824785469.png "title=" 2016-09-28_223834.png "alt=" Wkiom1fr1arsanrqaacgc9tpk10382.png-wh_50 "/>

From this, it can be seen that running this project will only generate a Java file, stating that running this project is equivalent to running only one page, click Open Java file to see the source code:

/* * generated by the jasper component of apache tomcat *  version: apache tomcat/7.0.41 * generated at: 2016-09-28 14:18:14  utc * note: the last modified time of this file was  set to *       the last modified time  of the source file after *       generation  to assist with modification tracking. */package org.apache.jsp;import  javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;p Ublic final class  index_jsp extends org.apache.jasper.runtime.httpjspbase    implements  org.apache.jasper.runtime.JspSourceDependent {  private static final  javax.servlet.jsp.jspfactory _jspxfactory =           Javax.servlet.jsp.JspFactory.getDefaultFactory ();  private static java.util.map< Java.lang.string,java.lang.long> _jspx_dependants;  static {    _ Jspx_dependants = new java.util.hashmap<java.lang.string,java.lang.long> (2);     _jspx_dependants.put ("/top.jsp",  long.valueof (1475071806944L));     _jspx_ Dependants.put ("/footer.jsp",  long.valueof (1475071854827L));  }  private  javax.el.expressionfactory _el_expressionfactory;  private  org.apache.tomcat.instancemanager _jsp_instancemanager;  public java.util.map< Java.lang.string,java.lang.long> getdependants ()  {    return _jspx_ Dependants;  }  public void _jspinit ()  {    _el_exprEssionfactory = _jspxfactory.getjspapplicationcontext (Getservletconfig (). GetServletContext ()). Getexpressionfactory ();    _jsp_instancemanager =  Org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager (Getservletconfig ());  }   public void _jspdestroy ()  {  }  public void _jspservice ( final javax.servlet.http.httpservletrequest request, final  Javax.servlet.http.httpservletresponse response)         throws  java.io.IOException, javax.servlet.ServletException {    final  javax.servlet.jsp.pagecontext pagecontext;    javax.servlet.http.httpsession  session = null;    final javax.servlet.servletcontext application;     final javax.servlet.ServletConfig config;    Javax.servlet.jsp.jspwriter out = null;    final java.lang.object  page = this;    javax.servlet.jsp.JspWriter _jspx_out =  null;    javax.servlet.jsp.pagecontext _jspx_page_context = null;     try {      response.setcontenttype ("text/html;  Charset=utf-8 ");       pagecontext = _jspxfactory.getpagecontext (This,  request, response,      null, true, 8192, true);       _jspx_page_context = pageContext;       Application = pagecontext.getservletcontext ();       config =  pagecontext.getservletconfig ();      session =  Pagecontext.getsession ();     &nBsp; out = pagecontext.getout ();      _jspx_out = out;       out.write ("\ r \ n");       out.write ("<! Doctype html public \ "-//w3c//dtd html 4.01 transitional//en\"  \ "http// Www.w3.org/tr/html4/loose.dtd\ ">\r\n");       out.write ("

From this source can be seen, index is two JSP files are included first, and then run.


Conclusion 1: Static inclusions are first included in the page code and then run.


Test 2:

In conclusion 1, it is concluded that the static inclusion involves code first and then runs, and since it contains the code, will there be a conflict between the included code? Let's test again to see if the code between the included pages will conflict. As long as the code to conflict, the simplest is to make the JSP instructions are not the same. Let's just change the top.jsp page code to change the encoding format in the instruction:

<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "pageencoding=" GBK "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >


Re-run the index.jsp page:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/88/38/wKiom1fr14ThLCg3AAClByvsC34896.png-wh_500x0-wm_3 -wmp_4-s_2427914897.png "title=" 1.png "alt=" Wkiom1fr14thlcg3aaclbyvsc34896.png-wh_50 "/>

After the operation will find an error, the error message is:

/footer.jsp (line:1, Column:2) Page Directive:illegal to has multiple occurrences of contentType with Differen T values (old:text/html; charset=utf-8, new:text/html; charset=gbk)

Conclusion 2: Because the static inclusion is to first include the page code in, and then run, so the page between the same label property configuration is different, there will be conflicts. The <% of at least two JSP pages @page contenttype= "Text/html;charset=utf-8"%> should remain consistent. Similarly, the included JSP page and the included JSP page are best without duplicate HTML tags. Otherwise, coverage will occur.


Test 3:

is it possible to pass parameters when statically contained? This time passing parameters, General people will think of address: <% @include file= "top.jsp?i=9"%> The answer is NO!!! Because the page is included directly at this point, it makes no sense to set the parameter pass. It can't even be compiled!


Test 4:

Now that the code is included directly, will the server be able to be shared by two pages by responding to the parameters passed by the request? The following is a simple verification of the address parameter.

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Output the I value in its single page to see if it can output 9:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/88/34/wKioL1fr2_ny1_NyAAAb4-9D3DQ749.png-wh_500x0-wm_3 -wmp_4-s_2981433368.png "title=" 2.png "alt=" Wkiol1fr2_ny1_nyaaab4-9d3dq749.png-wh_50 "/>


Conclusion 3: Three pages can receive the value from the test.jsp page via request, indicating that the three or page is sharing a request.



For JSP dynamic inclusion, read the JSP's static inclusion and dynamic inclusion in detail (ii)



This article is from the "Program Ape Story" blog, reproduced please contact the author!

A detailed explanation of the JSP's static inclusion and dynamic inclusion (i)

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.