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
Servlet Exception and Error Handling的更多相关文章
- Error Handling and Exception
The default error handling in PHP is very simple.An error message with filename, line number and a m ...
- 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 ...
- Error Handling
Use Exceptions Rather Than Return Codes Back in the distant past there were many languages that didn ...
- Clean Code–Chapter 7 Error Handling
Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...
- Java的Exception和Error面试题10问10答
在Java核心知识的面试中,你总能碰到关于 处理Exception和Error的面试题.Exception处理是Java应用开发中一个非常重要的方面,也是编写强健而稳定的Java程序的关键,这自然使它 ...
- beam 的异常处理 Error Handling Elements in Apache Beam Pipelines
Error Handling Elements in Apache Beam Pipelines Vallery LanceyFollow Mar 15 I have noticed a defici ...
- 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 ...
- Fortify漏洞之Portability Flaw: File Separator 和 Poor Error Handling: Return Inside Finally
继续对Fortify的漏洞进行总结,本篇主要针对 Portability Flaw: File Separator 和 Poor Error Handling: Return Inside Fina ...
- 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 ...
随机推荐
- nginx按日期分割日志
#!/bin/bash # Program:chenglee # Auto cut nginx log script. LOGS_PATH="/usr/local/nginx1.13/log ...
- 20145221高其_MSF基础应用
20145221高其_MSF基础应用 目录 概述 MS08-067漏洞攻击 MS11-050漏洞攻击 MS10-087漏洞攻击 辅助模块 概述 MSF的六种模块 Exploit模块 是利用发现的安全漏 ...
- sqlitestudio
SQLite数据库的特性 特点: 1.轻量级2.独立性,没有依赖,无需安装3.隔离性 全部在一个文件夹系统4.跨平台 支持众多操作系统5.多语言接口 支持众多编程语言6.安全性 事物,通过独占性和共享 ...
- bzoj2086: [Poi2010]Blocks DP,单调栈
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2086 思路 这就有点妙了 题目意思就是让你求平均数>=k的最长序列 先求出a[i] ...
- 【Spring Security】五、自定义过滤器
在之前的几篇security教程中,资源和所对应的权限都是在xml中进行配置的,也就在http标签中配置intercept-url,试想要是配置的对象不多,那还好,但是平常实际开发中都往往是非常多的资 ...
- 【Runtime Error】打开Matlib7.0运行程序报错的解决办法
1.在C盘建立一个文件夹temp,存放临时文件: 2.右键我的电脑-属性-高级系统设置-环境变量-系统变量,将TEMP.TMP的值改成C:\temp: 3.还是在第2步那里,新建变量,变量名称为BLA ...
- [ECharts] - ECharts使用中国地图
格式1: https://www.cnblogs.com/luna666/p/9007263.html (非官方) <!DOCTYPE html> <html lang=" ...
- ElasticSearch 笔记
ES集群脑裂出现的原因: 1:网络原因 内网一般不会出现此问题,可以监控内网流量状态.外网的网络出现问题的可能性大些. 2:节点负载 主节点即负责管理集群又要存储数据,当访问量大时可能会导致es实例反 ...
- 授权oAuth
使用Client Credentials Grant授权方式给客户端发放access token 只验证客户端(Client),不验证用户(Resource Owner),只要客户端通过验证就发acc ...
- 51nod 1215 数组的宽度(单调栈)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215 题意: 思路: 计算出以第i个数为最大值的区间范围,l_max[i ...