JSP部署tomcate时重复局部变量
<% User loginUser = (User)session.getAttribute("loginUser");
int userlevel = Integer.parseInt( (request.getAttribute("userlevel")).toString());
String status = (String)request.getAttribute("formstatus");
%>
根据用户级别我必须在同一页面上包含不同的文件 例如,
<% if(userlevel > 2){ %>
<%@include file="apr-part3.jsp"%>
<%}else{ %>
You have not permission to view this part
<% } %>
在 apr-part3.jsp
页面中,某些部分对于用户 3 和 4 来说是通用的 我必须再次检查 apr-part3.jsp
中的用户级别,如下所示 在 apr-part3.jsp
中,
<% User loginUser = (User)session.getAttribute("loginUser");
int userlevel = Integer.parseInt( (request.getAttribute("userlevel")).toString());
String status = (String)request.getAttribute("formstatus");
%>
<% if(userlevel==3){
do something
}if(userlevel==4){
do something
}
在 eclipse 中一切运行正常,但在 tomcat apache 上部署时 它给出了错误
org.apache.jasper.JasperException:无法编译 JSP 类:
jsp 文件 /jsp-page/apr-part3.jsp 中的第 2 行发生错误 重复的局部变量loginUser
如何避免这些错误
<% User loginUser = (User)session.getAttribute("loginUser");
int userlevel = Integer.parseInt( (request.getAttribute("userlevel")).toString());
String status = (String)request.getAttribute("formstatus");
%>
based on userlevel I have to include different file on the same page
for example
<% if(userlevel > 2){ %>
<%@include file="apr-part3.jsp"%>
<%}else{ %>
You have not permission to view this part
<% } %>
in apr-part3.jsp
page some part is common for user 3 and 4
I have to check user level in apr-part3.jsp
as follows again
in apr-part3.jsp
<% User loginUser = (User)session.getAttribute("loginUser");
int userlevel = Integer.parseInt( (request.getAttribute("userlevel")).toString());
String status = (String)request.getAttribute("formstatus");
%>
<% if(userlevel==3){
do something
}if(userlevel==4){
do something
}
Everything runs fine in eclipse but when deploying on tomcat apache
it gives the error
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 2 in the jsp file: /jsp-page/apr-part3.jsp
Duplicate local variable loginUser
How can I avoid these errors
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@include
将在编译时包含 JSP。所以它基本上最终会出现在同一个 Java 代码块中。您有 2 个选择:
代替。apr-part3.jsp
中的重新声明。更好的是,只使用 JSTL/EL。
同样,请求属性
loginUser
可通过${loginUser}
获取,status
可通过${formstatus}
获取。另请参阅如何避免 JSP 文件中出现 Java 代码?The
@include
will include the JSP during compile time. So it basically ends up in the same block of Java code. You have 2 options:<jsp:include>
instead.apr-part3.jsp
.Better yet, is to just use JSTL/EL.
Equivalently, the request attribute
loginUser
is available by${loginUser}
andstatus
by${formstatus}
. See also How to avoid Java code in JSP files?这是一个小问题,因为它与您的问题标题相关,但与您的示例代码主体无关。
将标签与 scriptlet 一起使用时也会出现此问题。我知道,我知道,这不是最佳做法,但它确实发生了。至少在WebLogic 10.2.3 中是这样。
如果您有一个标记将变量添加到页面上下文以便在 jsp 内使用,就会出现这种情况,例如:
如果您随后在 using jsp 内的 scriptlet 中声明一个与上下文上的属性名称同名的 var,那么您jsp 编译时出现重复的局部变量错误。例如:
原因是 jsp 编译的 servlet 包含注入的标记代码和样板代码,这些代码实际上已经使用从页面上下文获取的属性值创建了一个“String myvar”。因此,您的 scriptlet 声明实际上是多余的,因为数据已经可用。
检查预编译的 servlet 进行检查(Web 应用程序中的 WEB-INF/weblogic.xml jsp-descriptor 配置)。
This is a small aside as it relates to your question header but not your example body of code.
This problem can occur when using tags with scriptlet too. I know, I know, it's not best practice but, it happens. At least this is the case in WebLogic 10.2.3.
The situation arises if you have a tag that adds a variable to the page context for consumption within the jsp, such as:
If you then declare a var within scriptlet within the using jsp with the same name as the attribute name on the context then you get a duplicate local variable error on jsp compilation. For instance:
The reason being that the jsp's compiled servlet contains injected tag code and boilerplate code that actually already creates a "String myvar" with the value of the attribute taken from the page context. So, your scriptlet declaration is actually superfluous as the data is already available.
Check the pre-compiled servlet to check (WEB-INF/weblogic.xml jsp-descriptor config in your webapp).