简体   繁体   English

如何捕获从Jasper报告图像Servlet引发的异常

[英]How to catch exception raised from the Jasper report image servlet

My web application use JASPERREPORTS to compile and serve various reports. 我的Web应用程序使用JASPERREPORTS来编译和提供各种报告。 All the images present in the report draws from the ImageServlet net.sf.jasperreports.j2ee.servlets.ImageServlet . 报告中显示的所有图像均来自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>

The request for the image is something like this: 图像请求如下所示:

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

In the security audit of my application, it was raised that if the query parameters are altered like setting image=pxxx causes a 500 internal server error. 在我的应用程序的安全审核中,有人提出,如果更改查询参数(如设置image=pxxx会导致500个内部服务器错误。 So I need to handle this exception. 所以我需要处理这个异常。

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)

My question is, how can handle this exception? 我的问题是,如何处理此异常? The servlet comes along with the JASPER jar and I have no control over the code of the servlet. servlet随JASPER jar一起提供,我无法控制servlet的代码。 Please advice. 请指教。

Create your own servlet and modify the servlet-mapping for /image to point to that instead of the jasper servlet. 创建您自己的servlet并修改/image的servlet映射以指向该/image ,而不是碧玉servlet。

Your new servlet does not have to do any of the work, it can simply pass on the request to the jasper servlet, but it gives you the ability to insert an exception handler to catch the JRRuntimeException . 您的新servlet不必执行任何工作,它可以简单地将请求传递给jasper servlet,但是它使您能够插入异常处理程序以捕获JRRuntimeException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM