2019独角兽企业重金招聘Python工程师标准>>>

javax.servlet.HttpServlet

protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException
protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException

抛出了 ServletExceptionIOException

让我们写一个简单的Servlet MyExceptionServlet 看下当我们在web中抛出如上的异常会出现什么情况:

package com.journaldev.servlet.exception;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@WebServlet("/MyExceptionServlet")
public class MyExceptionServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {throw new ServletException("GET method is not supported.");}}

我们在浏览器中发出请求 _ localhost:8080/ServletExceptionHandling/MyExceptionServlet_,得到如下响应:

因为浏览器只能解析 HTML 文本,所以在web应用中抛出异常后,Servlet容器会将抛出的异常解析为 HTML文本对浏览器进行响应。不同的解析会在不同的web容器中有所不同。以上响应是tomcat响应的结果,如果使用其它容器 JBOSS 或者 Glassfish 得到的响应会有所不同。

以上响应将服务器商的详细的错误信息展示给用户没有任何实际意义,不利于用户操作体验和网站安全。

Servlet 异常处理

Servlet api允许们对 web异常进行自定义处理,对用户给出有效的响应。可以在web应用中给出多个异常处理类,为了简单起见,我们只创建一个类对所有的异常和错误进行响应。

package com.journaldev.servlet.exception;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@WebServlet("/AppExceptionHandler")
public class AppExceptionHandler extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {processError(request, response);}protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {processError(request, response);}private void processError(HttpServletRequest request,HttpServletResponse response) throws IOException {// Analyze the servlet exceptionThrowable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name");if (servletName == null) {servletName = "Unknown";}String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");if (requestUri == null) {requestUri = "Unknown";}// Set response content typeresponse.setContentType("text/html");PrintWriter out = response.getWriter();out.write("<html><head><title>Exception/Error Details</title></head><body>");if(statusCode != 500){out.write("<h3>Error Details</h3>");out.write("<strong>Status Code</strong>:"+statusCode+"<br>");out.write("<strong>Requested URI</strong>:"+requestUri);}else{out.write("<h3>Exception Details</h3>");out.write("<ul><li>Servlet Name:"+servletName+"</li>");out.write("<li>Exception Name:"+throwable.getClass().getName()+"</li>");out.write("<li>Requested URI:"+requestUri+"</li>");out.write("<li>Exception Message:"+throwable.getMessage()+"</li>");out.write("</ul>");}out.write("<br><br>");out.write("<a href=\"index.html\">Home Page</a>");out.write("</body></html>");}
}

在 web.xml 中增加如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"><display-name>ServletExceptionHandling</display-name><welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list><error-page><error-code>404</error-code><location>/AppExceptionHandler</location></error-page><error-page><exception-type>javax.servlet.ServletException</exception-type><location>/AppExceptionHandler</location></error-page>
</web-app>

通过如上配置,我们此时再发出 404请求和 错误请求会得到如下响应:

转载于:https://my.oschina.net/u/3238650/blog/1555160

Servlet 异常处理相关推荐

  1. Servlet异常处理

    http://jspengxue.iteye.com/blog/48364 Servlet异常处理  1. 声明异常处理  声明两种错误处理:HTTP错误代码的处理 和 指定程序中产生的java异常的 ...

  2. servlet 异常处理_Servlet异常和错误处理示例教程

    servlet 异常处理 有时候我写了一篇有关Java异常处理的文章,但是当涉及到Web应用程序时,我们需要的不仅仅是Java中的异常处理. Servlet异常 如果您注意到,doGet()和doPo ...

  3. Servlet入门篇(GenericServlet 类 - HttpServlet 类 -ServletConfig 接口 - HttpServletRequest 接口……)

    1.Servlet 简介 Servlet 对请求的处理和响应过程可分为以下几个步骤: (1)客户端发送请求至服务器端: (2)服务器将请求信息发送至 Servlet : (3)Servlet 生成响应 ...

  4. servlet面试常问问题_50个Servlet面试问答

    servlet面试常问问题 Servlet是Java EE的一个非常重要的主题,所有Web应用程序框架(例如Spring和Struts)都建立在它之上. 这使servlet成为Java访谈中的热门话题 ...

  5. servlet文件上传下载_Servlet上传文件和下载文件示例

    servlet文件上传下载 Java Web应用程序中的文件上载和下载以及常见任务. 由于最近我写了很多有关Java servlet的文章 ,因此我想提供一个使用servlet上传和下载文件的示例示例 ...

  6. 50个Servlet面试问答

    Servlet是Java EE的一个非常重要的主题,所有Web应用程序框架(例如Spring和Struts)都建立在它之上. 这使servlet成为Java访谈中的热门话题. 在这里,我提供了50个s ...

  7. Servlet上传文件和下载文件示例

    Java Web应用程序中的文件上载和下载以及常见任务. 由于最近我写了很多有关Java servlet的文章 ,因此我想提供一个使用servlet上传和下载文件的示例示例. 用例 我们的用例是提供一 ...

  8. Servlet笔记之(三)

    Servlet异常处理: 1:web应用中常见的几种异常 Http错误代码 可能产生的原因 401 用户权限不足 403 服务器已经结束客户端的请求,未作出相应的相应(等待客户端的进一步操作) 404 ...

  9. jsp错误处理页面_JSP异常处理– JSP错误页面

    jsp错误处理页面 Exception handling in JSP is done by JSP exception pages. JSP异常页面中完成了JSP中的异常处理. JSP中的异常处理 ...

最新文章

  1. Entity Framework 6 Recipes 2nd Edition(10-5)译 - 在存储模型中使用自定义函数
  2. 关于 element 的 backToTop
  3. 设计模式之委派模式及适配器模式
  4. 编程之美-寻找最近点方法整理
  5. [转] 没人把程序员当回事儿
  6. numpy的使用数组的创建2
  7. 前端学习(628):数字类型
  8. python判断字符类型编程_Python检测数据类型的方法总结
  9. 作者:李超(1988-),男,上海交通大学硕士生,主要研究方向为大数据网络。...
  10. Kerberos学习(一)
  11. 下面哪个选项不是oracle用户,作业三(有答案)
  12. web安全day5:DNS部署与安全
  13. arduino环境下用ESP32连接PS2手柄
  14. Docker可视化工具——Portainer全解
  15. 目前最值得入手的蓝牙耳机有哪些?四款高性价比蓝牙耳机推荐
  16. Quantopian教程系列三
  17. OSChina 周六乱弹 ——你知道妹子喜欢什么了么?
  18. 178、锐捷交换机恢复出厂和各种基本配置
  19. XSS Challenges/刷题/Stage #3
  20. HTML——背景属性

热门文章

  1. 关于《红楼梦》的读后感优秀范文2000字
  2. jackson json转对象 对象转json
  3. mac下的apache项目发布出现403
  4. discuz如何去掉论坛的网页后缀名称forum.php
  5. 【leetcode】Remove Linked List Elements(easy)
  6. atmega8 Flash的使用
  7. 不可不读的绝对英文经典
  8. mysql版本 时间_【MySQL】MySQL版本时间线和MySQL各版本的区别
  9. 在ubuntu16.04中安装apache2+modsecurity以及自定义WAF规则详解
  10. 将英文man替换为中文man最简单的方法