日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Servlet 异常处理

發(fā)布時(shí)間:2025/7/14 66 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Servlet 异常处理 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

javax.servlet.HttpServlet

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

拋出了 ServletExceptionIOException

讓我們寫一個(gè)簡(jiǎn)單的Servlet MyExceptionServlet 看下當(dāng)我們?cè)趙eb中拋出如上的異常會(huì)出現(xiàn)什么情況:

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.");}}

我們?cè)跒g覽器中發(fā)出請(qǐng)求 _ localhost:8080/ServletExceptionHandling/MyExceptionServlet_,得到如下響應(yīng):

因?yàn)闉g覽器只能解析 HTML 文本,所以在web應(yīng)用中拋出異常后,Servlet容器會(huì)將拋出的異常解析為 HTML文本對(duì)瀏覽器進(jìn)行響應(yīng)。不同的解析會(huì)在不同的web容器中有所不同。以上響應(yīng)是tomcat響應(yīng)的結(jié)果,如果使用其它容器 JBOSS 或者 Glassfish 得到的響應(yīng)會(huì)有所不同。

以上響應(yīng)將服務(wù)器商的詳細(xì)的錯(cuò)誤信息展示給用戶沒有任何實(shí)際意義,不利于用戶操作體驗(yàn)和網(wǎng)站安全。

Servlet 異常處理

Servlet api允許們對(duì) web異常進(jìn)行自定義處理,對(duì)用戶給出有效的響應(yīng)。可以在web應(yīng)用中給出多個(gè)異常處理類,為了簡(jiǎn)單起見,我們只創(chuàng)建一個(gè)類對(duì)所有的異常和錯(cuò)誤進(jìn)行響應(yīng)。

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>

通過(guò)如上配置,我們此時(shí)再發(fā)出 404請(qǐng)求和 錯(cuò)誤請(qǐng)求會(huì)得到如下響應(yīng):

轉(zhuǎn)載于:https://my.oschina.net/u/3238650/blog/1555160

總結(jié)

以上是生活随笔為你收集整理的Servlet 异常处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。