zoukankan      html  css  js  c++  java
  • Servlet Exception and Error Handling

      Servlet API support for custom Exception and Error Handler servlets that we can congiure in deployment descriptor. The whole purpose of these ervlets are to hadle the Exception or Error raised by application and send useful HTML response to user. We can provide link to application home page or some details to let user know what went wrong. Using error-page element to set web.xml and in the exceptionHandler servlet.

      Each error-page element should have either error-code or exception-type element. We define the excepiton handler sdervlet in location element.

      Based on configuration in web.xml,  if the application throw 404 error or ServletException, it will be handled by AppExceptionHandler servlet.

      When such exception and error scenario appears, servlet container will invoke the corresponding HTTP method of the Excption Handler servlet and pass the request and response object.

      Before servlet container invokes the servlet to handle the exception, it sets some attributes in the request to get useful informatiuon ablout the exception, some of them are  javax.servlet.error.exception, javax.servlet.error.status_code, javax.servlet.error.servlet_name and javax.servlet.error.request_uri.

      For exception, status code is always 500 that corresponds to the "Internal Server Error", for other types of error we get different error codes such as 404,430 etc.

      Using the status code, ouir implementation presents different types of HTML response to the user. It also provides a hyperlink to the homepage of the application.

    Now when we will hiut our servlet that is throwing ServletException, we will get a response like below image.

    If we try to access an invalid URL that result in 404 response, we will get response like below image:

     

    Doesn't it look good and helps user to easily understand what happened and provides them a way to go to the correct location.

    It also avoids sending application sensitive information to the user.

    We should always have exception handers in place for our web application.

    If you want to handle runtime exceptions and all other exceptions and all other exceptions in a single exception handler, you can provide exception-type as Throwable.

    If there are multiple error-page entries, Let's say one for Throwable and one for IOException and application throws

    宛如智障,暗藏锋芒
  • 相关阅读:
    BotVS开发基础—2.7 指标MACD
    BotVS开发基础—2.7 指标MA
    BotVS开发基础—2.5 策略交互
    BotVS开发基础—2.5 绘制图表
    BotVS开发基础—2.5 状态信息显示表格
    BotVS开发基础—2.4 获取订单、取消订单、获取未完成订单
    BotVS开发基础—2.3 下市价单 交易
    BotVS开发基础—2.2 下限价单 交易
    BotVS开发基础—2.1 账户、行情、K线、深度
    python的eval函数
  • 原文地址:https://www.cnblogs.com/zienzir/p/9042048.html
Copyright © 2011-2022 走看看