首先定义一个被包含的页面:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定义一个被包含的页面</title>
</head>
<body>
<%
int x=10;
%>
<h1>
include.jsp页面当中的x的值为:--<%=x %>
</h1>
</body>
</html>
接下来定义一个静态的包含页面文件,对上述所创建的include.jsp文件进行包含操作
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定义静态包含处理页面</title>
</head>
<body>
<%
int x=100;//定义一个整形变量
%>
<h1>
include_demo04.jsp---<%=x %>
</h1>
<%@include file="include.jsp" %> 此处在编译时将会出现错误
</body>
</html>
当对静态的包含页面进行执行操作时,将会出现一个500错误,即程序错误。如下图:
出现以上错误的根本愿意是因为静态包含是将所有的内容在编译的时候都包含到一个文件夹当中,然后再一起执行。这样的话,对于变量x来说就相当于在一个程序当中被定义了两次所以就会出现程序错误。
接下来定义一个动态包含的jsp文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>定义动态的包含处理页面</title>
</head>
<body>
<%
int x=100;
%>
<h1>
include_demo05当中的x的值为:--<%=x %>
</h1>
<jsp:include page="include.jsp"/>
</body>
</html>
对于上述程序则不会出现上述的错误,以为在动态的包含中如果被包含的页面是动态的话,则会先对被包含的动态页面当中的应用型程序尽心处理,即
<% %>当中的代码进行处理,将处理后的结果即x的输出值和页面当中的静态内容一起返回到动态包含页面当中,然后在和动态包含页面当中的内容再一次进行接下来的任务操作。即动态包含页面当中所包含的只是被包含页面的处理后的结果。
动态页面的运行结果为: