开发者

JSP duplicate local variable while deploying tomcate

<% 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.ap开发者_如何学Pythonache.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


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:

  • Use <jsp:include> instead.
  • Get rid of the redeclarations in apr-part3.jsp.

Better yet, is to just use JSTL/EL.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:choose>
    <c:when test="${userlevel > 2}">
        <jsp:include page="apr-part3.jsp" />
    </c:when>
    <c:otherwise>
        You have not permission to view this part.
    </c:otherwise>
</c:choose>

Equivalently, the request attribute loginUser is available by ${loginUser} and status by ${formstatus}. See also How to avoid Java code in JSP files?


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:

public int doStartTag() throws JspException {
    String myvar = ...;
    pageContext.setAttribute("myvar", myvar);

    return EVAL_BODY_INCLUDE;
}

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:

<%
    String myvar = (String)pageContext.getAttribute("myvar");
    ...
 %>

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜