说明:包含指令分为静态包含和动态包含
静态包含
语法:<@ include file="页面路径"/>
实例:include.jsp文件静态包含info.jsp 和info.html两个文件
info.jsp代码文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>info.jsp</h1>
</body>
</html>
info.html代码文件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>info.html</h1>
</body>
</html>
include.jsp代码文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
+++++++++++++++++++++++++++++++++
<%@ include file="info.html" %>
+++++++++++++++++++++++++++++++++
<%@ include file="info.jsp" %>
+++++++++++++++++++++++++++++++++
</body>
</html>
我们访问include.jsp页面后查看页面源代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
+++++++++++++++++++++++++++++++++
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>info.html</h1>
</body>
</html>
+++++++++++++++++++++++++++++++++
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>info.jsp</h1>
</body>
</html>
+++++++++++++++++++++++++++++++++
</body>
</html>
总结:从上面的代码可以看出,静态包含指令就是直接把所包含的页面原封不动的拿过来。然后再编译输出,如果两个页面中定义了相同的变量名称。那么页面就会出
错(实例中未演示)。
口诀:
静态包含 先包含 再处理
动态包含
语法:
<jsp:include page="info2.jsp">
<jsp:param value="Tom" name="userName"/>
<jsp:param value="123" name="password"/>
</jsp:include>
=============================================
<jsp:include page="info2.html"></jsp:include>
实例:include2.jsp动态包含两个页面,分别是info2.html 和info2.jsp 其中info2.html文件和上面的info.htm的内容一致。剩余两个文件的内容如下:
include2.jsp页面代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
=============================================
<jsp:include page="info2.jsp">
<jsp:param value="Tom" name="userName"/>
<jsp:param value="123" name="password"/>
</jsp:include>
=============================================
<jsp:include page="info2.html"></jsp:include>
</body>
</html>
info2.jsp页面代码:
<%@ page language="java" contentType="text/html;"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>info2.jsp</h1>
<%
String userName=request.getParameter("userName");
String password=request.getParameter("password");
%>
用户名:<%=userName %>
<br/>密码:<%=password %>
</body>
</html>
总结:info2.jsp将接收到include2.jsp传递过来的用户名和密码。当这两个页面中定义的变量名称相同时,页面不会出错(实例中未演示)
口诀:动
态包含 先处理 再包含