JSP-include指令
用法:
<%@ include file="路径" %>
a1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>
<h2>a1 Page</h2>
</body>
</html>
a2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>
<h2>a2 Page</h2>
<%@ include file="/WEB-INF/a1.jsp"%>
</body>
</html>
运行:
动态包含和静态包含的区别
静态包含:
a1.jsp
<h2>a2 Page</h2>
<%
String s = "abcd";
%>
<%@ include file="/WEB-INF/a1.jsp"%>
a2.jsp
<h2>a1 Page</h2>
<%= s %>
运行:
动态包含:
用法:
<jsp:include page="路径"></jsp:include>
a2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>
<h2>a2 Page</h2>
<jsp:include page="/WEB-INF/a1.jsp"></jsp:include>
</body>
</html>
a1.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>
<h2>a1 Page->动态包含</h2>
</body>
</html>
运行:
但是如果使用刚才的例子,会出现空指针异常,因为动态包含其实在
底层生成的是两个java文件,而静态包含是一个java文件
动态包含传递参数:
a2.jsp:
<h2>a2 Page</h2>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:include page="/WEB-INF/a1.jsp">
<jsp:param value="猴赛雷" name="name"/>
</jsp:include>
a1.jsp
<h2>a1 Page->动态包含</h2>
<%= request.getParameter("name") %>
运行: