<jsp:include> 和<%@ include %>的区别
使用<%@ include %>指令元素,只是将页面的内容静态的包含进来,如果被包含的文件中有jsp代码,则会执行该代码,不管该文件是否为动态文件;
<%@ page contentType="text/html;charset=utf-8" %>
<html>
<head><title>包含了一段jsp代码的txt文件</title></head>
<body>
当前日期与时间:<%@ include file="content.txt" %>
</body>
</html>
-----------------
<%@ page import="java.util.*" %>
<%=new Date() %>
<!-- 此时的jsp代码会被执行,显示出时间-->
+++++++++++++++++++++++++++++++++++++++++++
<%@ page contentType="text/html;charset=utf-8" %>
<html>
<head><title>包含了一段jsp代码的txt文件</title></head>
<body>
当前日期与时间:<jsp:include page="content.txt" />
</body>
</html>
<!-- 此时的jsp代码不会被执行,不能够正常显示时间-->
<jsp:param>动作元素
<jsp:param>动作元素是用来传递参数,一般与<jsp:include>和<jsp:forward>联合使用
<jsp:param name="参数名" value="参数值" />
<jsp:param>动作元素包含两个属性,name用来设定传递参数的名称,value用来设定传递参数的值;
示例代码:-----------------------
<%@ page contentType="text/html";charset="utf-8" %>
<html>
<head><title>包含jsp文件并传递参数</title></head>
<body>
<jsp:include page="contentDemo.jsp">
<jsp:param name="age" value="19" />
</jsp:include>
</body>
</html>
contentDemo.jsp--------------------------
<%@ page contentType="text/html";charset="utf-8" %>
<h2>被包含页面</h2>
<p>接受到的参数</p>
<%
String strAge = request.getParameter("age");
%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body style="font-size:30px;">
<jsp:forward page="content.jsp">
<jsp:param value="tom" name="name"/>
<jsp:param value="19" name="age"/>
<jsp:param value="man" name="sex"/>
</jsp:forward>
</body>
</html>
content.jsp---------------------
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body style="font-size:30px;">
<% Date now = new Date();
out.print(now);%>
<%
String Name = request.getParameter("name");
String Age = request.getParameter("age");
String Sex = request.getParameter("sex");
%>
<%="name的参数是:"+Name %><br/>
<%="age的参数是:"+Age %>
<%="sex的参数是:"+Sex %>
</body>
</html>
使用<%@ include %>指令元素,只是将页面的内容静态的包含进来,如果被包含的文件中有jsp代码,则会执行该代码,不管该文件是否为动态文件;
<%@ page contentType="text/html;charset=utf-8" %>
<html>
<head><title>包含了一段jsp代码的txt文件</title></head>
<body>
当前日期与时间:<%@ include file="content.txt" %>
</body>
</html>
-----------------
<%@ page import="java.util.*" %>
<%=new Date() %>
<!-- 此时的jsp代码会被执行,显示出时间-->
+++++++++++++++++++++++++++++++++++++++++++
<%@ page contentType="text/html;charset=utf-8" %>
<html>
<head><title>包含了一段jsp代码的txt文件</title></head>
<body>
当前日期与时间:<jsp:include page="content.txt" />
</body>
</html>
<!-- 此时的jsp代码不会被执行,不能够正常显示时间-->
<jsp:param>动作元素
<jsp:param>动作元素是用来传递参数,一般与<jsp:include>和<jsp:forward>联合使用
<jsp:param name="参数名" value="参数值" />
<jsp:param>动作元素包含两个属性,name用来设定传递参数的名称,value用来设定传递参数的值;
示例代码:-----------------------
<%@ page contentType="text/html";charset="utf-8" %>
<html>
<head><title>包含jsp文件并传递参数</title></head>
<body>
<jsp:include page="contentDemo.jsp">
<jsp:param name="age" value="19" />
</jsp:include>
</body>
</html>
contentDemo.jsp--------------------------
<%@ page contentType="text/html";charset="utf-8" %>
<h2>被包含页面</h2>
<p>接受到的参数</p>
<%
String strAge = request.getParameter("age");
%>
<%="age 参数值为:"+strAge%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body style="font-size:30px;">
<jsp:forward page="content.jsp">
<jsp:param value="tom" name="name"/>
<jsp:param value="19" name="age"/>
<jsp:param value="man" name="sex"/>
</jsp:forward>
</body>
</html>
content.jsp---------------------
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body style="font-size:30px;">
<% Date now = new Date();
out.print(now);%>
<%
String Name = request.getParameter("name");
String Age = request.getParameter("age");
String Sex = request.getParameter("sex");
%>
<%="name的参数是:"+Name %><br/>
<%="age的参数是:"+Age %>
<%="sex的参数是:"+Sex %>
</body>
</html>