Servlet 异常处理
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拋出了 ServletException 和 IOException。
讓我們寫一個簡單的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 异常处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于jetty的那些奇葩问题
- 下一篇: 我的第一个 Mono for Andro