Jsp out.print()捕获了额外的文本

时间:2014-02-05 12:20:13

标签: java jsp printing

我的Jsp中有代码片段:

catch (Exception e) {
  out.print("<Grid><IO Result='-1' Message='");
  out.print(e.toString());
  out.print("'/></Grid>");
  out.close();
  throw new Exception("");
}

我希望结果必须是有效的xml

"<Grid><IO Result='-1' Message='Failed to load HSQLDB JDBC driver.'/></Grid>"

但结果如下:

"<Grid><IO Result='-1' Message='");  out.print(e.toString());  out.print("'/></Grid>"

请帮我解决这个问题!

2 个答案:

答案 0 :(得分:0)

catch (Exception e) {
    %><Grid><IO Result='-1' Message='%><%= e.getMessage() %><%'/></Grid><%
    throw new Exception("");
}

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:catch var ="e">
    ... // All
</c:catch>
<c:if test = "${e!=null}">
    <Grid>
        <IO Result='-1' Message='%><c:out escapeXML="true" value="${e.message}"/><%'/>
    </Grid>
</c:if>

投掷我会留下来。

答案 1 :(得分:0)

第一个out.print()中的单引号未关闭。你可以移动e.toString()吗?

编辑:并转义单引号?

catch (Exception e) {
 out.print("<Grid><IO Result=\'-1\' Message=\'" + e.toString() + "\'/>");
 out.print("</Grid>");
 out.close();
 throw new Exception("");
}