jsp 文件中的以下代码使用 Apache Tomcat 毫无问题地显示网页:
<!DOCTYPE HTML>
<%@page import='java.util.*' %>
<%@page contentType='text/html' pageEncoding='UTF-8' %>
<html>
<body>
<%
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("string A");
stringList.add("string B");
for (int i = 0; i < stringList.size(); i++){
out.println("<p>"+stringList.get(i)+"</p>");
}
%>
</body>
</html>
但是这段使用 XML 语法的代码会抛出 JasperException,即使它与上面的相同,只是将 JSP 标签替换为 JSP XML 标签:
<!DOCTYPE HTML>
<jsp:directive.page import='java.util.*' />
<jsp:directive.page contentType='text/html' pageEncoding='UTF-8' />
<html>
<body>
<jsp:scriptlet>
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("string A");
stringList.add("string B");
for (int i = 0; i < stringList.size(); i++){
out.println("<p>"+stringList.get(i)+"</p>");
}
</jsp:scriptlet>
</body>
</html>
抛出这个错误:
Aug 22, 2013 2:27:42 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [] threw exception [/cftp2Error.jsp (line: 6, column: 17) Unterminated <jsp:scriptlet> tag] with root cause
org.apache.jasper.JasperException: /cftp2Error.jsp (line: 6, column: 17) Unterminated <jsp:scriptlet> tag
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:408)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:133)
...
谁能告诉我我的 XML JSP 代码哪里出了问题?我在 Tomcat 7.0.34 上运行 JDK1.7。 谢谢, 基思
最佳答案
您需要将文件内容附在<jsp:root></jsp:root>
中并转义 < 和 > 字符,例如:
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("string A");
stringList.add("string B");
for (int i = 0; i < stringList.size(); i++){
out.println("<p>"+stringList.get(i)+"</p>");
}
</jsp:scriptlet>
</body>
关于java - JSP XML 语法错误 : JasperException when replacing <% %> with <jsp:scriptlet> </jsp:scriptlet>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18371783/