您的位置:首页 > Web前端 > JavaScript

(九)包含指令 (JSP学习第二天)

2011-09-26 15:59 429 查看
静态包含:<%@ include file="要包含的文件的路径">

定义三个要包含的文件info.html,info.jsp,info.inc
info.html文件内容:

<h1>
<font color="red">info.html</font>
</h1>


info.jsp文件内容:

<h1>
<font color="green"><%="info.jsp"%></font>
</h1>


info.inc文件内容:

<h1>
<font color="yellow">info.inc</font>
</h1>


使用静态包含三个文件:include_demo01.jsp

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>测试</title>
</head>
<body>
<h1>静态包含</h1>
<%@include file="info.html" %>
<%@include file="info.jsp" %>
<%@include file="info.inc" %>
</body>
</html>


动态包含:
<jsp:include page="包含的文件"/>
传递参数的:
<jsp:include page="包含的文件">
<jsp:param name="参数名称" value="参数内容"/>
.........
</jsp:include>

用动态包含三个文件:include_demo02.jsp

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>测试</title>
</head>
<body>
<h1>动态包含</h1>
<jsp:include page="info.html"/>
<jsp:include page="info.jsp"/>
<jsp:include page="info.inc"/>
</body>
</html>


定义包含文件,并传递参数 include_demo03.jsp

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>测试</title>
</head>
<body>
<%
String username="xiaoming";
%>
<jsp:include page="receive_param.jsp"/>
<jsp:param name="name" value="<%=username%>"/>
<jsp:param name="info" value="ceshi"/>
</body>
</html>


接收传递的参数:receive_param.jsp

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>测试</title>
</head>
<body>
<h1>参数一:<%=request.getParameter("name")%></h1>
<h2>参数二:<%=request.getParameter("info")%>
</body>
</html>


开发时用动态包含,可以避免一些不必要的错误,比如变量重复定义

例如:被包含的动态页include.jsp
<%
int x=10;
%>
<h1>include.jsp--<%=x%></h1>

静态包含处理页:include_demo04.jsp

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>测试</title>
</head>
<body>
<%
int x=100;
%>
<%@ include file="include.jsp"%>
</body>
</html>


运行时报错,是因为相同的变量重复定义!

动态包含处理页:include_demo05.jsp

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>测试</title>
</head>
<body>
<%
int x=100;
%>
<jsp:include page="include.jsp"/>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: