为什么JSP中的Java代码会产生错误?

时间:2012-01-11 09:27:40

标签: java jsp exception

我正在尝试在我的jsp中输入一些java代码,但是生成了以下异常:

org.apache.jasper.JasperException: /Home.jsp(31,8) PWC6203: Scripting elements ( <%!, <jsp:declaration, <%=, <jsp:expression, <%, <jsp:scriptlet ) are disallowed

我的jsp包含if语句并将检查数据。如果匹配,则显示一些html代码,否则显示另一个代码

<% String username = session.getAttribute("loggedIn").toString();
               String actual = "${message.message}";
               if(username.equals(actual)){%>
                <div style="background-color:#fff380;"> 
                ...
                </div>
            <%} else { %>
                <div> 
                ...
                </div>
            <%}%>

有谁知道为什么会产生这种类型的错误?非常感谢

1 个答案:

答案 0 :(得分:2)

Sciptlet的使用可能配置无效(请参阅http://www.java-samples.com/showtutorial.php?tutorialid=548)。这个选择是明智的选择,因为不再需要在JSP中使用scriptlet。使用JSTL和EL:

<c:choose>
    <c:when test="${loggedIn == message.message}">
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

此外,即使scriplet有效,也不能在scriptlet代码中使用JSP EL。