日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Servlet 异常处理

發布時間:2025/7/14 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

拋出了 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 异常处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。