JSP页面使用include指令出现 Duplicate local variable basePath
2024-09-18 20:26:27
现有三个页面 " 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变量重复,这就是之前的错误提示表达的意思。
最新文章
- CSharpGL(27)讲讲清楚OpenGL坐标变换
- STM32 奇偶校验配置
- 【架构】How To Use HAProxy to Set Up MySQL Load Balancing
- Linux 中文乱码问题解决
- php程序员的水平 看看自己属于那个级别的
- elasticsearch Java API汇总
- ANT 环境搭建
- Java的正则表达式
- 网页爬虫--scrapy入门
- 安装mysql之后,存入中文出现乱码
- js 截取某个字符前面或者后面的字符串
- OpenWrt固件刷入后串口终端没有反应的问题
- C++中变量自动初始化的问题
- 哆啦A梦连连看游戏源码完整版
- 以后坚持用java
- website
- poj 2431
- .NET 多语言支持解决方案 (转)
- poj 3422 Kaka&#39;s Matrix Travels 费用流
- CRtmpServer分析与应用