jsp包含分为动态包含和静态包含。
动态包含:<jsp:include page=""></jsp:include>先编译在包含可以包含动态页面(jsp)和静态页面(html)
静态包含:<include file=""></include>只能包含静态页面
运行顺序:按照引用的顺序运行的,谁先引用,谁执行。所有的页面都被系统编写到一个大的页面上,按照顺序执行。
注意:在子页面如果调用JS方法,如果该JS写在子页面是可以的,但是如果该方法写在子页面的引用文件*.js时,要注意在子页面上调用时正常的,但是到主页面上去加载时,不能正常调用了,提示找不到该方法,原因是应该把该JS方法写在主页面的*.JS文件里面。
如果在子页面要引用JS方法,都应该把JS方法写在主页面的JS文件里面,这样才能正常调用。
大家仔细看下面的例子:
主indexhtml:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript">
i=0;//主页面上定义全局变量i的值
i++;
alert(i);
</script>
</head>
<body>
<b>我是测试静态包含的主页面</b><br/>
<%@include file="html/1.html"%>
<%@include file="html/2.html"%>
</body>
</html>
子页面1.html:
<!DOCTYPE html>
<html>
<head>
<title>1.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
i++;//此时i的值改变
alert(i);
</script>
<script type="text/javascript" src="js/1.js"></script>//如果主文件包含子文件则调用这个路径,这就相当于这个JS是相当于子文件的路径
<!-- <script type="text/javascript" src="../js/1.js"></script>-->//这个是单独运行子文件的时候,JS文件相当于子文件的路径
</head>
<body>
1111111111111111111.html<br>
</body>
</html>
同理可得动态包含对于js的处理也是这个样子的。
如果动态包含里面含有java代码,无论子文件还是主文件,系统都会主动先去编译一遍,如果有错误会报错的。这叫做先编译后执行。
主indexjsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'indexjsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
我是动态包含主页面 <br>
<jsp:include page="jsp/1.jsp"></jsp:include>
<%
int i=88;
%>
<%=i %>
</body>
</html>
子1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP '1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%int i=0; %>
<body>
<%=i %>
11111111111111.jsp<br>
</body>
</html>