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. kali linux 2.0配置更新源后apt-get update 报错

    这个是我/etc/apt/sources.list的更新源: deb http://http.kali.org/kali kali-rolling main contrib non-free deb ...

  2. ubuntu查看文件和文件夹大小

    在实际使用ubuntu时候,经常要碰到需要查看文件以及文件夹大小的情况. 有时候,自己创建压缩文件,可以使用 ls -hl 查看文件大小.参数-h 表示Human-Readable,使用GB,MB等易 ...

  3. https://blog.csdn.net/dayancn/article/details/54692111

    Ubuntu恢复被删除的文件 昨天一不小心,执行了rm xx  -rf,把一个项目删除了.然后就是各种悔恨,各种自责,这个项目可是一周的工作量啊.最后肯定得解决,于是google搜索发现了恢复神器ex ...

  4. IDEA安装与破解

    今天下午偶然在知乎上看到IDEA和eclipse的软件分析,所以装了一个IDEA,不过肯定是破解,不会购买激活码 IDEA官网:http://www.jetbrains.com/idea/ 安装教程: ...

  5. oracle数据库数据库表空间查询及扩充

    1.查询表空间,及表空间的大小 SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size FROM dba_tabl ...

  6. Ping 不通的原因分析

    背景 简介 ping是常用的网络管理命令,ping也属于一个通信协议,是TCP/IP协议的一部分,适用于windows和linux以及unix 根据reply用来检查网络是否通畅&网络连接的速 ...

  7. [HDU 1976] Software Version

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1976 #include<iostream> #include<cstdio> ...

  8. Unity3D学习笔记(三十一):Xlua(1)

    Xlua:腾讯研发,开源免费   配置:文件解压,拷贝到Unity项目里 注意:Xlua文件夹不许移动,不许重命名   运行Xlua: 1.引用命名空间 2.创建虚拟机 3.运行lua语句 4.不需要 ...

  9. zepto 入门

    2017-03-17 文章来源:http://www.cnblogs.com/daysme zepto 简介 jq虽然有一些衍生的插件可用在移动端上,但它有点大. click 有,但有问题 onmou ...

  10. 【译】第43节---EF6-自定义约定

    原文:http://www.entityframeworktutorial.net/entityframework6/custom-conventions-codefirst.aspx Code-Fi ...