理解JSP中的include(一):include-file 指令测试
发表于:2007-07-04来源:作者:点击数:
标签:
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 % %
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显示出错。
%>
结果:
dec_str
scr_str
结论:file1中定义的实例变量(或局部变量),file2可以引用并更改。但直接访问file2会出错。
第二个例子:
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"; %>
查看file1 对应的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_depend
ants;
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;
String temp = "temp";
……
out.print(scr_str);
temp = "hello " + temp;
……
out.print(temp);
} catch (Throwable t) {
……
} finally {
……
}
}
}
结果:
hello scr_str
hello temp
结论:file2中定义的实例变量,file1可以在<%@ include file="" %>指令之前(或之后)引用并更改。
file2中定义的局部变量,file1必须在<%@ include file="" %>指令之后引用并更改。
第三个例子:
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对应的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) {
……
}
结果:
str1 str2 str1 str2
结论:file2并不生成servlet,但其中的内容都被包含在了file1生成的servlet之中;file2本身也被添加进名为“ _jspx_dependants”的Vector类型的类变量中。可以看到,在file2中定义的实例变量“scr_str ”被转移到了file1生成的servlet中。可见
Tomcat是以一种很耐人寻味的机制进行处理的。
原文转自:http://www.ltesting.net