jsp基础之--jsp的静态、动态包含
2016-09-17 21:34
447 查看
最近学校在上Java Web,讲到了静态、动态包含,在这里做一个小结。
1、新建一个工程,叫include-test
2、新建一个index.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 '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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%@include file = "1.jsp" %>
This is my JSP page <br>
</body>
</html>
3、再新建一个1.jsp页面,内容如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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>
<body>
<%out.println("Hello"); %>
</body>
</html>
这个小例子做的是一个静态包含的效果
运行效果如下
![](https://img-blog.csdn.net/20160917214505748)
在例子中,用到
<%@include file = "1.jsp" %>指令,该指令将会在主页面index.jsp中静态包含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 'index2.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>
<body>
<jsp:include page="2.jsp"/>
This is my JSP page. <br>
</body>
</html>
2、新建一个2.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 '2.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>
<body>
This is my JSP 2 page. <br>
</body>
</html>
这个小例子用的是动态包含的效果
运行效果如下
![](https://img-blog.csdn.net/20160917214742389)
在例子中,用到
<jsp:include page="2.jsp"/>
指令,该指令会在主页index2.jsp中动态包含2.jsp页面
1、新建一个工程,叫include-test
2、新建一个index.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 '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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%@include file = "1.jsp" %>
This is my JSP page <br>
</body>
</html>
3、再新建一个1.jsp页面,内容如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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>
<body>
<%out.println("Hello"); %>
</body>
</html>
这个小例子做的是一个静态包含的效果
运行效果如下
在例子中,用到
<%@include file = "1.jsp" %>指令,该指令将会在主页面index.jsp中静态包含1.jsp页面
静态包含的特点是:被包含页面中不能与主页面有相同的变量名、方法名,否则会报错。也就是说,1.jsp在运行前就与index.jsp合为一体了
1、新建一个页面,叫index2.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 'index2.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>
<body>
<jsp:include page="2.jsp"/>
This is my JSP page. <br>
</body>
</html>
2、新建一个2.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 '2.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>
<body>
This is my JSP 2 page. <br>
</body>
</html>
这个小例子用的是动态包含的效果
运行效果如下
在例子中,用到
<jsp:include page="2.jsp"/>
指令,该指令会在主页index2.jsp中动态包含2.jsp页面
动态包含的特点是:被包含页面和主页面是相互独立的,他们只是在运行的时候相融合,所以在两个页面中可以有相同的变量、方法名
相关文章推荐
- JSP基础语法之三:动态、静态两种包含指令
- jsp中静态包含和动态包含小结
- JSP静态包含和动态包含问题
- JSP的动态包含与静态包含
- [Jweb] JSP-编程 03 静态, 动态包含
- jsp静态包含与动态包含的区别
- jsp静态包含和动态包含的区别
- jsp的动态包含和静态包含
- JSP静态包含与动态包含的区别
- 测试Jsp 静态包含和动态包含
- jsp动态包含,静态包含
- jsp中静态包含与动态包含
- JSP中的动态包含和静态包含的区别
- JSP中的静态包含和动态包含
- JSP动态包含与静态包含
- jsp中静态包含和动态包含的区别与联系
- JSP的动态包含和静态包含详解
- jsp页面中的动态包含和静态包含
- JSP静态包含和动态包含
- 初学jsp之动态包含与静态包含