现有三个页面 " include.jsp " " a.jsp " " b.jsp "
页面代码如下
首先是a.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%out.println("I am a.jsp"); %>
b.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%out.println("I am b.jsp"); %>
include.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="a.jsp" %>
/*此处报错
- Duplicate local variable path
- Duplicate local variable basePath
- Duplicate local variable path
*/ <jsp:include page="b.jsp"></jsp:include> </body> </html>
但是把
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<base href="<%=basePath%>">
这两段代码删除就不会报错了
查看网上资料后了解到:include元素指令的作用发生在转换阶段,它会把a.jsp的代码包含进index.jsp;include动作指令的作用发生在执行阶段,它不会把b.jsp的代码包含到index.jsp中,而是在执行时向b.jsp发出请求,把得到的结果包含到index.jsp中。因此,查看a.jsp的源代码,发现其中有定义path,basePath变量,既然是包含源代码,那么就会和index.jsp中定义的path,basePath变量重复,这就是之前的错误提示表达的意思。