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

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>
这个小例子做的是一个静态包含的效果
运行效果如下



在例子中,用到

<%@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 基础