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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Web.xml配置Error Page不能够转发的问题分析及解决

發布時間:2024/1/17 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Web.xml配置Error Page不能够转发的问题分析及解决 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.??????web.xml的配置

自定義的異常類

1 <error-page> 2 <exception-type>com.service.Exception.MyException</exception-type> 3 <location>/my_exception.jsp</location> 4 </error-page>

???定義HTTP消息狀態碼

<error-page><error-code>404</error-code><location>/404.html</location> </error-page>

2.??????出錯原因分析

1)??IE將出錯頁面響應狀態碼200,告訴瀏覽器是成功消息,顯示該頁面,如下:

1 <% 2 response.setStatus(200); // 200 = HttpServletResponse.SC_OK 3 %>

2)??如果是上面的錯誤相信Google很容易得到,但是對于JSP頁面中拋出的異常(RuntimeException),還有可能是如下原因:

a.當通過request分發請求my.jsp頁面,my.jsp頁面中的某個方法會拋出MyException,代碼如下:getRequestDispatcher(“/my.jsp”).forward(request, response);在這里,整個程序執行的流程為:分發dispather時,Tomcat容器會生成一個my.jsp的包裝類,實際就是JspServletWrapper通過getServlet方法得到myServlet.java執行service方法,拋出異常,在這里自定義的異常MyException將會被HandleException方法處理,最后拋出JasperException,而不是我們自己定義的異常。如下代碼

JspServletWrapper類中:

?

1 2 3 servlet = getServlet(); 4 5 try { 6 7 /* 8 9 * (3) Service request 10 11 */ 12 13 if (servlet instanceof SingleThreadModel) { 14 15 // sync on the wrapper so that the freshness 16 17 // of the page is determined right before servicing 18 19 synchronized (this) { 20 21 servlet.service(request, response); 22 23 } 24 25 } else { 26 27 //這里調用my.jsp生成的Servlet.service,執行拋出自定義的異常,最后被catch轉換成JasperException 28 29 servlet.service(request, response); 30 31 } 32 33 34 35 } catch (UnavailableException ex) { 36 37 String includeRequestUri = (String) 38 39 request.getAttribute("javax.servlet.include.request_uri"); 40 41 if (includeRequestUri != null) { 42 43 // This file was included. Throw an exception as 44 45 // a response.sendError() will be ignored by the 46 47 // servlet engine. 48 49 throw ex; 50 51 } else { 52 53 int unavailableSeconds = ex.getUnavailableSeconds(); 54 55 if (unavailableSeconds <= 0) { 56 57 unavailableSeconds = 60; // Arbitrary default 58 59 } 60 61 available = System.currentTimeMillis() + 62 63 (unavailableSeconds * 1000L); 64 65 response.sendError 66 67 (HttpServletResponse.SC_SERVICE_UNAVAILABLE, 68 69 ex.getMessage()); 70 71 } 72 73 } catch (ServletException ex) { 74 75 if(options.getDevelopment()) { 76 77 throw handleJspException(ex); 78 79 } else { 80 81 throw ex; 82 83 } 84 85 } catch (IOException ex) { 86 87 if(options.getDevelopment()) { 88 89 throw handleJspException(ex); 90 91 } else { 92 93 throw ex; 94 95 } 96 97 } catch (IllegalStateException ex) { 98 99 if(options.getDevelopment()) { 100 101 throw handleJspException(ex); 102 103 } else { 104 105 throw ex; 106 107 } 108 109 } catch (Exception ex) { 110 111 if(options.getDevelopment()) { 112 113 throw handleJspException(ex); 114 115 } else { 116 117 throw new JasperException(ex); 118 119 } 120 121 }

所以當用Dispather分發頁面時,如果頁面中有異常拋出,最終將被轉換成JasperException,如果通過Web.xml去handle,必須攔截JasperException才能夠正確跳轉。

b.當通過重定向跳轉到my.jsp頁面,代碼如下,response.sendRedirect(“/my.jsp”)或者直接在瀏覽器地址欄上輸入URL,實際上是JspServlet執行my_jsp.java的service方法,代碼片段如下:

1 try { 2 3 boolean precompile = preCompile(request); 4 5 serviceJspFile(request, response, jspUri, null, precompile); 6 7 //這里對于RuntimeException直接拋出 8 9 } catch (RuntimeException e) { 10 11 throw e; 12 13 } catch (ServletException e) { 14 15 throw e; 16 17 } catch (IOException e) { 18 19 throw e; 20 21 } catch (Throwable e) { 22 23 throw new ServletException(e); 24 25 }

3.??????解決辦法

最后解決辦法,是在web.xml中配置對應的Handle自定義的Exception,還需要加如下代碼,如下:

1 try{ 2 3 getRequestDispatcher(“/my.jsp”).forward(request, response); 4 5 }catch(Exception e){ 6 7 if(e.getCause() instanceof MyException){ 8 9 response.sendRedirect(“/error.jsp”); 10 11 } 12 13 }

如果改為response.sendRedirect(“/my.jsp”),直接在web.xml攔截即可。

轉載于:https://www.cnblogs.com/wangbt/archive/2012/05/16/2505433.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Web.xml配置Error Page不能够转发的问题分析及解决的全部內容,希望文章能夠幫你解決所遇到的問題。

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