Understanding include in JSP (i): include-file instruction Test

Source: Internet
Author: User
Tags final include throwable
Include|js First Example:

INCLUDE-FILE-TEST-1.JSP:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%! String dec_str = "Dec_str";%>
<%=dec_str%><br>
<%@ include file= "include-file-test-2.jsp"%><br>
<%=dec_str%><br>

INCLUDE-FILE-TEST-2.JSP:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%
Dec_str = "Scr_str"; Eclipse displays an error.
%>

Results:

Dec_str

Scr_str

Conclusion: the instance variable (or local variable) defined in File1, file2 can be referenced and changed. But direct access to File2 can be an error.



A second example:

INCLUDE-FILE-TEST-1.JSP:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>

<% scr_str = "Hello" + scr_str;%>

<%@ include file= "include-file-test-2.jsp"%>
<%=scr_str%><br>
<% temp = "Hello" + temp;%>
<%=temp%>

INCLUDE-FILE-TEST-2.JSP:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%! String scr_str = "Scr_str";%>
<% String temp = "temp"; %>

View the file1 corresponding servlet:

Package org.apache.jsp.jsp_002dsyntax_002dtest;

Import ... ;

Public final class include_002dfile_002dtest_002d1_jsp ... {

String scr_str = "Scr_str";
private static Java.util.Vector _jspx_dependants;

static {
_jspx_dependants = new Java.util.Vector (1);
_jspx_dependants.add ("/jsp-syntax-test/include-file-test-2.jsp");
}

Public Java.util.List getdependants () {
return _jspx_dependants;
}

public void _jspservice (HttpServletRequest request, httpservletresponse response)
Throws Java.io.IOException, Servletexception {

......
try {
......

Response.setcontenttype ("TEXT/HTML;CHARSET=GBK");
......

Scr_str = "Hello" + scr_str;
The String temp = "temp";
......
Out.print (SCR_STR);
temp = "Hello" + temp;
......

Out.print (temp);
catch (Throwable t) {
......

finally {
......
}
}
}

Results:

Hello Scr_str
Hello Temp

Conclusion: the instance variable defined in File2, file1 can be referenced and changed before (or after) the <%@ include file= ""%> directive.

Local variables defined in File2, file1 must be referenced and changed after the <%@ include file= ""%> directive.



A third example:

INCLUDE-FILE-TEST-1.JSP:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%!
String str1 = "str1";
%>
<%
String STR4 = str1 + str2 + str3;
%>
<%@ include file= "include-file-test-2.jsp"%>
<%=str4%>

INCLUDE-FILE-TEST-2.JSP:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%!
String str2 = "str2";
String STR3 = str1 + str2;
%>

File1 corresponds to the servlet:

Package org.apache.jsp.jsp_002dsyntax_002dtest;

Import ...

Public final class include_002dfile_002dtest_002d1_jsp ... {


String str1 = "str1";


String str2 = "str2";
String STR3 = str1 + str2;

......

public void _jspservice (HttpServletRequest request, httpservletresponse response)
Throws Java.io.IOException, Servletexception {

......


try {
......
Response.setcontenttype ("TEXT/HTML;CHARSET=GBK");
......

String STR4 = str1 + str2 + str3;

......
Out.print (STR4);
catch (Throwable t) {
......
}




Results:

STR1 str2 str1 str2



Conclusion: File2 does not generate a servlet, but its contents are included in the File1-generated servlet, and file2 itself is added to a vector-type class variable named "_jspx_dependants". As you can see, the instance variable "SCR_STR" defined in File2 is moved to the File1-generated servlet. It can be seen that Tomcat is dealt with in a very intriguing mechanism.




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.