首先创建了一个Jsp页面date.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
Date d = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
String s = sf.format(d);
out.print(s);
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
然后创建了一个测试include指令的页面include_Command.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'include.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>
<h1>Include指令</h1>
<%@ include file="date.jsp" %>
<hr>
</body>
</html>
然后就报了这两个错:“Duplicate local vatiable basePath”和“Duplicate local vatiable path” Errors
其实是重复声明了变量 basePath和path
因为 <%@ include file="date.jsp" %>
是将file指定的页面代码完全放入到你的页面中,这样,相当于声明了两次
!!!
分析:<%@ include file="" %>和<jsp:include page=""></jsp:include>区别与分析
<%@ include file="" %>是将文件原封不动的copy进现有的文件中,像是拼接好后,再编译成为servlet运行。
<jsp:include page=""></jsp:include>是编译后的servlet运行到该句时,跳转到指定的jsp编译的那个servlet继续运行,然后将运行结果,copy到现在的jsp中,故包含与被包含文件都是单独运行的。
那么怎么解决呢?有两种解决方法:1、可以把其中一个页面的basePath和path的声明语句删掉或者注释掉
2、如果考虑到后面可能要用到这两个变量可以修改下变量名让它们不重复就行了