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

Servlet Exception and Error Handling的更多相关文章

  1. Error Handling and Exception

    The default error handling in PHP is very simple.An error message with filename, line number and a m ...

  2. setjmp()、longjmp() Linux Exception Handling/Error Handling、no-local goto

    目录 . 应用场景 . Use Case Code Analysis . 和setjmp.longjmp有关的glibc and eglibc 2.5, 2.7, 2.13 - Buffer Over ...

  3. Error Handling

    Use Exceptions Rather Than Return Codes Back in the distant past there were many languages that didn ...

  4. Clean Code–Chapter 7 Error Handling

    Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...

  5. Java的Exception和Error面试题10问10答

    在Java核心知识的面试中,你总能碰到关于 处理Exception和Error的面试题.Exception处理是Java应用开发中一个非常重要的方面,也是编写强健而稳定的Java程序的关键,这自然使它 ...

  6. beam 的异常处理 Error Handling Elements in Apache Beam Pipelines

    Error Handling Elements in Apache Beam Pipelines Vallery LanceyFollow Mar 15 I have noticed a defici ...

  7. CAS (10) —— JBoss EAP 6.4下部署CAS时出现错误exception.message=Error decoding flow execution的解决办法

    CAS (10) -- JBoss EAP 6.4下部署CAS时出现错误exception.message=Error decoding flow execution的解决办法 jboss版本: jb ...

  8. Fortify漏洞之Portability Flaw: File Separator 和 Poor Error Handling: Return Inside Finally

    继续对Fortify的漏洞进行总结,本篇主要针对 Portability Flaw: File Separator 和  Poor Error Handling: Return Inside Fina ...

  9. Error handling in Swift does not involve stack unwinding. What does it mean?

    Stack unwinding is just the process of navigating up the stack looking for the handler. Wikipedia su ...

随机推荐

  1. 获取RadioButton选中的值

    1.RadioButtonList的RepeatDirection="Horizontal"可以设置按扭选项横对齐: 2.获取选中的RadioButton值; $("#& ...

  2. QT---实现舒尔特方格(零基础入门)

    按照之前说的,加上舒尔特方格,读者还可以自行将此游戏做成APP放到手机上,后面还有贪吃蛇,Java版的飞机大战,五子棋,各类游戏会不断加上来的,当然,会免费附加源代码! 读者可以去4399去玩一下,可 ...

  3. Regsvr32 在64位机器上的用法(转载)

    转载:http://blog.csdn.net/xuzhimin1991/article/details/65436864 regsvr32是windows上注册 OLE 控件(DLL 或 Activ ...

  4. topcoder srm 694 div1 -3

    1.给出$n$个数字,将其分成三个非空的组,每组的权值为该组所有数字的抑或.选择一种分法使得三组的权值和最大? 思路:记录前两组的权值且三组有没有数字时第三组的值.(当前两组的值知道时第三组的权值是确 ...

  5. 通过SIMPLE_DEV_PM_OPS定义suspend和resume函数【转】

    本文转载自:https://blog.csdn.net/tiantao2012/article/details/77851782 通过SIMPLE_DEV_PM_OPS 定义这个驱动的suspend和 ...

  6. 通过cmd调用Powershell脚本

    一共需要3个文件,把这3个文件放在一个路径下 UTF8NoBOM.bat   这个文件是为了调用ps1 pwsh -file "%cd%\UTF8NoBOM.ps1" UTF8No ...

  7. (zhuan) How to Train Neural Networks With Backpropagation

    this blog from: http://blog.demofox.org/2017/03/09/how-to-train-neural-networks-with-backpropagation ...

  8. ssh中的 Connection closed by ***

    另一台电脑的 mac/windows10/win7 都可以连接,就这台电脑不可以,但是能 ping 通, ssh 时总是 Connection reset by xxx 或 Connection cl ...

  9. js捕获错误

    文: http://www.jb51.net/article/78764.htm 用window.onerror捕获并上报Js错误的方法 前两天有个2048游戏的用户反馈说,打开游戏后不能玩儿,只有一 ...

  10. 在GeoServer里设置图层的默认自定义样式,出现不显示预览图的情况(不起作用)

    在GeoServer里设置图层的默认自定义样式 点击"Layers-->world:country"图层,点击"Publishing"标签,在下面的&qu ...