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. 转:网卡、光纤网卡、ISCSI卡有什么区别?

    网卡.光纤网卡.ISCSI卡有什么区别? 感谢: http://www.it.com.cn/f/server/063/6/241650.htm 在讨论这个问题的时候,需要先说清楚一个问题:我们知道,在 ...

  2. 20145308 《网络对抗》 Web应用 学习总结

    20145308 <网络对抗> Web应用 学习总结 实验内容 (1)Web前端HTML (2)Web前端javascipt (3)Web后端:MySQL基础:正常安装.启动MySQL,建 ...

  3. Codeforces 37D Lesson Timetable - 组合数学 - 动态规划

    题目传送门 神奇的门I 神奇的门II 题目大意 有$n$组学生要上课2次课,有$m$个教室,编号为$1$到$m$.要确定有多少种不同的安排上课的教室的方案(每组学生都是本质不同的),使得它们满足: 每 ...

  4. Qt重绘机制

    一.引发重绘的事件 1.调用repaint() 2.调用uodate() 二.控件hide或者show 三.其他 ps: repaint函数是立即重绘,没有优化 update会优化,异步重绘,所以如果 ...

  5. Python3基础 list extend 合并列表

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. Android系统分区理解及分区目录细解【转】

    本文转载自:https://blog.csdn.net/u010001503/article/details/51853822 Android 通常有以下分区: System分区: 就是我们刷ROM的 ...

  7. LIS|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9; int f[N], a[N]; int n; // ...

  8. (转)Awesome GAN for Medical Imaging

    Awesome GAN for Medical Imaging 2018-08-10 09:32:43 This blog is copied from: https://github.com/xin ...

  9. ComponentOne 2017 V1 发布

    在刚刚庆祝完Visual Studio20周年之后,我们迎来了ComponentOne 2017年第一个重要的版本. ComponentOne Studio与Visual Studio 2017配合发 ...

  10. CentOS7使用firewalld和selinux

    转载自莫小安的博客:https://www.cnblogs.com/moxiaoan/p/5683743.html 如何查看和使用selinux https://blog.csdn.net/edide ...