0

我的 Web 应用程序使用 JASPERREPORTS 来编译和提供各种报告。报告中出现的所有图像均来自 ImageServlet net.sf.jasperreports.j2ee.servlets.ImageServlet

<servlet>
    <servlet-name>ImageServlet</servlet-name>
    <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ImageServlet</servlet-name>
    <url-pattern>/image</url-pattern>
</servlet-mapping>

对图像的请求是这样的:

GET /watapp/image?rand=0.5039537955900038&image=px

在我的应用程序的安全审计中,有人提出如果查询参数被更改,例如设置 image=pxxx会导致 500 内部服务器错误。所以我需要处理这个异常。

net.sf.jasperreports.engine.JRRuntimeException: Invalid image name: pxxx
        at net.sf.jasperreports.engine.export.JRHtmlExporter.getPrintElementIndex(JRHtmlExporter.java:2408)
        at net.sf.jasperreports.engine.export.JRHtmlExporter.getImage(JRHtmlExporter.java:695)
        at net.sf.jasperreports.j2ee.servlets.ImageServlet.service(ImageServlet.java:98)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at com.watapp.filters.login.ApplicationFilter.doFilter(ApplicationFilter.java:41)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:662)

我的问题是,如何处理这个异常?servlet 与 JASPER jar 一起提供,我无法控制 servlet 的代码。请指教。

4

1 回答 1

0

创建您自己的 servlet 并修改 servlet-mapping for/image以指向它而不是 jasper servlet。

您的新 servlet 不必做任何工作,它可以简单地将请求传递给 jasper servlet,但它使您能够插入异常处理程序来捕获JRRuntimeException.

于 2013-01-22T16:05:01.363 回答