jsp异常处理_JSP异常处理

jsp异常处理

Exception Handling is a process of handling exceptional condition that might occur in your application. Exception Handling in JSP is much easier than Java Technology exception handling. Although JSP Technology also uses the same exception class objects.

异常处理是处理您的应用程序中可能发生的异常情况的过程。 JSP中的异常处理比Java技术异常处理容易得多。 尽管JSP Technology还使用相同的异常类对象。

It is quite obvious that you dont want to show error stack trace to any random user surfing your website. You can't prevent all errors in your application but you can atleast give a user friendly error response page.

很明显,您不想向浏览您的网站的任何随机用户显示错误堆栈跟踪。 您无法防止应用程序中的所有错误,但至少可以提供一个用户友好的错误响应页面。

在JSP中执行异常处理的方法 (Ways to perform Exception Handling in JSP)

JSP provide 3 different ways to perform exception handling:

JSP提供了3种不同的方式来执行异常处理:

  1. Using isErrorPage and errorPage attribute of page directive.

    使用page指令的isErrorPageerrorPage属性。

  2. Using <error-page> tag in Deployment Descriptor.

    Deployment Descriptor中使用<error-page>标记。

  3. Using simple try...catch block.

    使用简单的try...catch块。

isErrorPage和errorPage属性的示例 (Example of isErrorPage and errorPage attribute)

isErrorPage attribute in page directive officially appoints a JSP page as an error page.

页面指​​令中的isErrorPage属性正式将JSP页面指定为错误页面。

error.jsp

error.jsp

Exception handling in Jsp

errorPage attribute in a page directive informs the Web Container that if an exception occurs in the current page, forward the request to the specified error page.

页面指​​令中的errorPage属性通知Web容器,如果当前页面中发生异常,则将请求转发到指定的错误页面。

sum.jsp

sum.jsp

Exception handling in Jsp

Whenever an exception occurs in sum.jsp page the user is redirected to the error.jsp page, where either you can display a nice message, or you can also print the exception trace into a file/database in the background, to check later what caused the error.

每当sum.jsp页中发生异常时,用户就会被重定向到error.jsp页,您可以在其中显示一条漂亮的消息,也可以在后台将异常跟踪记录打印到文件/数据库中,以便以后查看导致了错误。

在部署描述符中声明错误页面 (Declaring error page in Deployment Descriptor)

You can also declare error pages in the DD for the entire Web Apllication. Using <error-page> tag in the Deployment Descriptor. You can even configure different error pages for different exception types, or HTTP error code type(503, 500 etc).

您还可以在DD中为整个Web应用程序声明错误页面。 在Deployment Descriptor中使用<error-page>标记。 您甚至可以为不同的异常类型或HTTP错误代码类型(503、500等)配置不同的错误页面。

Declaring an error page for all type of exception

为所有类型的异常声明一个错误页面

<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>

Declaring an error page for more detailed exception

声明错误页面以获取更详细的异常

<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/error.jsp</location>
</error-page>

Declaring an error page based on HTTP Status code

声明基于HTTP状态码的错误页面

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

使用try ... catch块 (Using the try...catch block)

Using try...catch block is just like how it is used in Core Java.

使用try...catch块就像在Core Java中一样。

<html>
<head>
   <title>Try...Catch Example</title>
</head>
<body>
   try{
      int i = 100;
      i = i / 0;
      out.println("The answer is " + i);
   }
   catch (Exception e){
      out.println("An exception occurred: " + e.getMessage());
   }
  %>
</body>
</html>

翻译自: https://www.studytonight.com/jsp/exception-handling-in-jsp.php

jsp异常处理

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值