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. hdu 4366 Successor - CDQ分治 - 线段树 - 树分块

    Sean owns a company and he is the BOSS.The other Staff has one Superior.every staff has a loyalty an ...

  2. 安装旧版本的Firefox

    在Ubuntu上安装就版本的Firefox,此处以version 46为例子: 1.删除已存在的Firefox, On CentOS/RHEL # yum remove firefox On Ubun ...

  3. Spring 学习——Spring框架结构、概念

    maven项目结构 记忆:在一个项目中,project下一层级时src,也就是源文件,所有需要进行编译的文件都是在这个目录下,其实也就是这一个目录,然后向下扩展.在src目录下,存在main文件夹,里 ...

  4. Python3基础 dict 创建字典 空字典

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

  5. 【做题】Codeforces Round #436 (Div. 2) F. Cities Excursions——图论+dfs

    题意:给你一个有向图,多次询问从一个点到另一个点字典序最小的路径上第k个点. 考虑枚举每一个点作为汇点(记为i),计算出其他所有点到i的字典序最小的路径.(当然,枚举源点也是可行的) 首先,我们建一张 ...

  6. 检测浏览器(BOM)以及地址栏网址的API

    navigator.userAgent //检测浏览器的版本以及那个厂商的 (不怎么准,你比如360经常跟别人干架,所以别人检测到360浏览器就提示浏览器危险,所以360就自己修改了) //分解这个地 ...

  7. ActiveReports 大数据分析报告:公交车司乘冲突引发的刑事案件

    公交车司乘冲突刑事案件总体情况 公交车司乘冲突引发的刑事案件总量稳中有升 (注:本报告界定的“公交车司乘冲突刑事案件”,是指案件诱因为公交车司机与乘客在乘车过程中发生冲突而引发的刑事案件.包括但不限于 ...

  8. QT读文件夹内所有文件名

    void monizhuzhan::filenameInDir() { //判断路径是否存在 QDir dir(path); if(!dir.exists()) return; //查看路径中后缀为. ...

  9. 为DataGridView增加鼠标滚轮功能

    #region 鼠标滚动 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "Wi ...

  10. _event_worldstate

    EventId 事件ID ID WorldStateUI.dbc第10列数字部分 StartValue 起始值 Entry 更新世界状态需要击杀生物或摧毁物体的entry,正数为生物,负数为物体 St ...