简体   繁体   English

如何不在浏览器上打印stacktrace

[英]How not to print stacktrace on the browser

I am working on a web service. 我正在使用Web服务。 Sometimes during my testing exceptions are thrown and displayed on the browser page. 有时,在我进行测试时,异常会抛出并显示在浏览器页面上。 I am not sure why even the code that "could" throw an exception is within the try-catch block, the exception is still printed in the browser. 我不确定为什么即使“可能”引发异常的代码也位于try-catch块中,但异常仍显示在浏览器中。

I certainly can create exception handler with Spring or exception mapper (with JBoss) but still there are exceptions that could be thrown that are not mapped, and I don't want this to be printed on the browser. 我当然可以使用Spring或异常映射器(使用JBoss)创建异常处理程序,但是仍然会抛出未映射的异常,并且我不希望将其打印在浏览器上。

How can I fix this? 我怎样才能解决这个问题?

You might be catching checked exceptions and those shown on the browser might be unchecked exceptions. 您可能正在捕获已检查的异常,而浏览器上显示的异常可能是未检查的异常。 One way is to catch RuntimeException which is the parent for all unchecked exceptions and display a meaningful error in the browser. 一种方法是捕获RuntimeException,它是所有未检查的异常的父级,并在浏览器中显示有意义的错误。 If you are the owner of the APIs throwing such exceptions, it is worth going through this small notes on exceptions . 如果您是引发此类异常的API的所有者,则值得阅读有关这些异常的小注释。

in your web.xml you can define error pages like this 在您的web.xml中,您可以定义这样的错误页面

<error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/error.jsp</location>
</error-page>

<error-page>
    <error-code>403</error-code>
    <location>/WEB_INF/jsp/accessDenied.jsp</location>
</error-page>

and so on... you can have for any other error code especially error 500 witch i think is more suitable for your case 等等......你可以有任何其他错误代码错误尤其是巫500,我认为是更适合你的情况

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

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