JSP Servlet中的最佳实践错误处理 [英] Best practice error handling in JSP Servlets

查看:100
本文介绍了JSP Servlet中的最佳实践错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的网络应用程序,使用jsp / servlets通过Oracle数据库。该应用程序现在只有少量页面,尽管它正在增长。



截至目前,我没有显示用户错误的内容。例如,如果数据库连接失败,则页面只是空白。我好奇最好的做法是错误处理?我应该打印一个堆栈跟踪吗?所有错误都应该转到默认错误页面?



任何建议或参考资料都不胜感激。正如你所说,这对我来说是一种新的东西。



谢谢

解决方案>

对于被认为是不可恢复的错误(例如数据库连接问题),这些错误通常被捕获在应用程序中的最高级别,并在一个地方处理。许多框架将把它们转换成未经检查的exeptions,以避免中间层必须处理它们。



对于这些不可恢复的异常,通常你会显示一个用户友好的和相当通用的错误页面给用户,并将堆栈跟踪(具有更详细的信息)发送到日志文件 - 供系统管理员和/或开发人员查询。



servlet规范提供通过 web.xml 通过错误页面标记来处理错误的方法。



如果您使用Servlet 3.0或更高版本,那么在 web.xml 中,您可以添加: p>

 < error-page> 
< location> /error.html< / location>
< / error-page>

这将捕获所有未处理的异常,并将它们发送到 error.html 页面在webapp的根目录。



在早期版本的servlet规范中,您必须指定异常类型或错误代码仍然为了更精细的粒度错误处理):

 < error-page> 
< exception-type> java.lang.Exception< / exception-type>
< location> /error.html< / location>
< / error-page>

或:

 code><错误页> 
< error-code> 500< / error-code>
< location> /error.html< / location>
< / error-page>

And:

 code><错误页> 
< error-code> 404< / error-code>
< location> /notFound.html< / location>
< / error-page>

如果您需要在其中执行动态处理,则可以转发到另一个JSP(或另一个servlet)错误页面:

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

如果您需要从错误页面中访问异常(也许您想显示一些特定数据由例外(例如代码)持有,那么您可以通过 javax.servlet.error.exception 请求属性访问原始异常:

  Throwable throwable =(Throwable)request.getAttribute(javax.servlet.error.exception); 

如果您的应用程序不断增长,您可能最好转到MVC框架,例如 Spring MVC - 这将构建您的应用程序更易于管理 - 加上它将为错误处理提供一致和明确的机制。


I have a pretty simple web app using jsp/servlets over an Oracle database. The app only has a handful of pages right now, though it is growing.

As of now, I dont have anything that shows users errors. For example, if the database connection fails, the page is just blank. I'm curious what the best practices are error handling? Should I print out a stack trace? Should all errors goto a default error page?

Any advice or reference material would be appreciated. As you might tell, this is kind of new to me.

Thanks

解决方案

For errors that are considered to be unrecoverable (such as database connectivity problems), these sorts of errors are typically caught at the top-most level within the application and dealt with in a single place. Many frameworks will convert these to unchecked exeptions to avoid intermediate layers having to deal with them.

For these unrecoverable exceptions, typically you'd display a user-friendly and fairly generic error page to the user and send a stacktrace (with more detailed information) to a log file - for interrogation by system administrators and/or developers.

The servlet spec provides a way to handle errors through the web.xml via the error-page tag.

If you're using Servlet 3.0 or above, then in your web.xml you can add:

<error-page>
    <location>/error.html</location>
</error-page>

That will catch all unhandled exceptions and send them to the error.html page in the root of the webapp.

In earlier versions of the servlet spec you had to specify the exception type or error code (which you can still do for finer grained error handling):

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/error.html</location>
</error-page>

Or:

<error-page>
    <error-code>500</error-code>
    <location>/error.html</location>
</error-page>

And:

<error-page>
    <error-code>404</error-code>
    <location>/notFound.html</location>
</error-page>

Plus you can forward to another JSP (or another servlet) if you need to do dynamic processing in the error page:

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

If you need to access the exception from inside your error page (perhaps you want to display some specific data held by the exception - such as a code) then you can access the original exception through the javax.servlet.error.exception request attribute:

Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");

If your app is growing, you may be best to move to an MVC framework - such as Spring MVC - which will make building your app more manageable - plus it will provide consistent and well defined mechanisms for error handling.

这篇关于JSP Servlet中的最佳实践错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆