????涓? 澹版??寮?寮?甯稿???
??????eb.xml涓?瀵瑰0??瀵瑰??绉?寮?甯哥??澶????規???
??????杩? ??绱??ュ0???? 姝ゅ??绱???缁???濡?涓?锛?
????+------ or
????|
???? ----+
????|
????+------
????1 HTTP??璇?浠g????澶???瀹??昏?淇?????
????4**浠g??琛ㄧず瀹㈡?風????璇?锛?璇鋒???璇?娉???璇?????璇鋒???娉?瀹??般??
????5**浠g??琛ㄧず???″?ㄧ????璇?锛????″?ㄦ???藉???闆??娉???璇鋒???
????渚?锛?涓?04??璇???瀹??稿?????璇?澶???椤甸??
??????eb.xml涓?
????
????404
????/FileNotFound.html
????
????缂???FileNotFound.html
????????涔???浠ョ???涓?涓?涓??ㄥ???HTTP??璇?浠g????HttpErrorHandlerServlet绫繪?ヨ?琛???搴?
????package servlet;
????import java.io.PrintWriter;
????import javax.servlet.ServletException;
????import javax.servlet.http.HttpServlet;
????import javax.servlet.http.HttpServletRequest;
????import javax.servlet.http.HttpServletResponse;
????public class HttpErrorHandlerServlet extends HttpServlet
????{
????protected void service(HttpServletRequest req, HttpServletResponse resp)
????throws ServletException, java.io.IOException
????{
????resp.setContentType("text/html;charset=GB2312");
????PrintWriter out = resp.getWriter();
????Integer status_code=(Integer)req.getAttribute("javax.servlet.error.status_code");
????out.println("");
????out.println("");
????//濡???浣???JDK????浣?浜?1.5锛??d?浣?搴?璇ユ???у?涓??瑰?璋???
????//int status=status_code.intValue();
????//switch(status){...}
????switch(status_code)
????{
????case 401:
????break;
????case 404:
????out.println("
HTTP?舵??浠g??锛?"+status_code+"
");
????out.println("浣???璁塊??椤甸?㈠苟涓?瀛????????宸茬?琚?绉誨?ㄥ?闆?跺??浣?缃???");
????out.println("
????
????break;
????default:
????break;
????}
????out.println("");
????out.close();
????}
????}
????????HttpServlet绫葷??service()?規???璋??ㄨ?鋒?瀵矽薄getAttribute()?規?寰???avax.servlet.error.status_code灞??х???鹼?褰?????HTTP??璇????跺??锛?
????Servlet瀹瑰?ㄤ????ㄥ?HTTP????璇?浠g??浣?涓?avax.servlet.error.status_code
????灞??х???鹼?淇?瀛??ㄨ?鋒???瀵矽薄涓???
?????ㄧ講濂借?涓?servlet??location淇??逛負servlet??location
????2 Java寮?甯哥??澶???
????渚?锛?涓?涓?Servlet浠???浠朵腑璇誨????缃?淇℃??锛?濡?????浠朵?瀛??ㄧ??锛?灏變?????ava.io.FileNotFoundException寮?甯搞??
????====绗?涓?姝ワ?FileExceptionServlet.java
????package org.sunxin.ch06.servlet;
????import java.io.FileInputStream;
????import java.io.IOException;
????import java.util.Properties;
????import javax.servlet.ServletException;
????import javax.servlet.http.HttpServlet;
????import javax.servlet.http.HttpServletRequest;
????import javax.servlet.http.HttpServletResponse;
????public class FileExceptionServlet extends HttpServlet
????{
????public void doGet(HttpServletRequest req, HttpServletResponse resp)
????throws ServletException,IOException
????{
????FileInputStream fis=new FileInputStream("config.inc");
????Properties pps=new Properties();
????pps.load(fis);
????//璇誨??灞??х??浠g??锛????ャ??
????fis.close();
????}
????}
????濡????句???onfig.inc??浠訛?FileExceptionServlet灏變?????ava.io.FileNotFoundException寮?甯?
????========绗?浜?姝ワ?寮?甯稿???Servlet绫彙??ExceptionHandlerServlet.java
????package servlet;
????import java.io.PrintWriter;
????import javax.servlet.ServletException;
????import javax.servlet.http.HttpServlet;
????import javax.servlet.http.HttpServletRequest;
????import javax.servlet.http.HttpServletResponse;
????public class ExceptionHandlerServlet2 extends HttpServlet
????{
????protected void service(HttpServletRequest req, HttpServletResponse resp)
????throws ServletException, java.io.IOException
????{
????resp.setContentType("text/html;charset=GB2312");
????PrintWriter out = resp.getWriter();
????out.println("");
????out.println("");
????String uri=(String)req.getAttribute("javax.servlet.error.request_uri");
????Object excep=req.getAttribute("javax.servlet.error.exception");
????out.println(uri+" 杩?琛???璇???");
????out.println("
??璇?????锛?"+excep);
????out.println("");
????out.close();
????}
????}
????========绗?涓?姝ワ?web.xml??浠朵腑澹版??Servlet
????
????java.io.FileNotFoundException
????/ExcepHandler
????
????瀛???绱???瀹?浜?Java寮?甯哥被????瀛?
????瀛???绱???瀹?浜?瀵瑰?甯稿?????Servlet绫?
????浜? 绋?搴?寮?寮?甯稿???
????绋?搴?寮?寮?甯稿???灏辨????eb绋?搴?涓??╃??ry-catch璇??ヨ?琛????峰?甯革?骞跺?規???峰?甯歌?琛???搴???澶?????
????1 ??ry-catch璇??ヤ腑澶???寮?甯?
????catch(SQLException se)
????{
????getServletContext().log("ServletContext.log(): ?版??搴???浣?澶辮觸!"+
????se.toString());
????log("GenericServlet.log(): ?版??搴???浣?澶辮觸!"+se.toString());
????resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
????"?版??搴???浣??虹?伴??棰?锛?璇瘋??绯葷?$??????");
????}
????[1] ?????闆?甯告?訛???????ervletContext?ュ?g??log()?規???GenericServlet?借薄绫葷??log()?規?璁闆??版??搴???浣?澶辮觸????????
?????哄??锛?
????濡???璋???enericServlet绫葷??log()?規? 瀹?浼??ㄦ?ュ?娑????????㈠??涓?Servlet????瀛?
??????璋???ervletContext?ュ?g??log()?規? ????璁闆?娑?????韬?
????杩?涓や釜log()?規?浼????ュ?淇℃?????ュ?版?ュ???浠朵腑?? ??omcat6 浜х?????ュ???浠跺??浠ュ????浠跺???劇??浣?缃???
????%CATALINA_HOME%\logs\localhost.褰????ユ??.log??
????[2] 璋??ㄥ??搴?瀵矽薄??sendError()?規?????HTTP??璇?浠g??锛?Servlet瀹瑰?ㄤ?????涓?涓?????浜?杩?浜?淇℃??????璇?椤甸?㈠?版?瑙???
????2 浣跨??equestDispatcher?ュ???寮?甯?
????浜х??寮?甯哥??Servlet
????try
????{
????int a=5;
????int b=0;
????int c=a/b;
????}
????catch(ArithmeticException ae)
????{
????req.setAttribute("javax.servlet.error.exception",ae);
????req.setAttribute("javax.servlet.error.request_uri",req.getRequestURI());
????RequestDispatcher rd=req.getRequestDispatcher("ExcepHandler2");
????rd.forward(req,resp);
????}
????澶???寮?甯哥??Servlet
????package servlet;
????import java.io.PrintWriter;
????import javax.servlet.ServletException;
????import javax.servlet.http.HttpServlet;
????import javax.servlet.http.HttpServletRequest;
????import javax.servlet.http.HttpServletResponse;
????public class ExceptionHandlerServlet2 extends HttpServlet
????{
????protected void service(HttpServletRequest req, HttpServletResponse resp)
????throws ServletException, java.io.IOException
????{
????resp.setContentType("text/html;charset=GB2312");
????PrintWriter out = resp.getWriter();
????out.println("");
????out.println("");
????String uri=(String)req.getAttribute("javax.servlet.error.request_uri");
????Object excep=req.getAttribute("javax.servlet.error.exception");
????out.println(uri+" 杩?琛???璇???");
????out.println("
??璇?????锛?"+excep);
????out.println("");
????out.close();
????}
????}
????璇存??锛?
????[1] 灏?寮?甯稿?矽薄?????哄?甯哥??servlet??浣?缃?浣?涓?ttpServletRequest瀵矽薄??灞??т?瀛??拌?鋒?瀵矽薄涓???
????[2] ??杩?璇鋒?瀵矽薄??getRequestDispatcher()?規?寰???equestDispatcher瀵矽薄 璋???equestDispatcher瀵矽薄??forward()?規?灏?璇鋒?杞???缁? ExcepHandler2
????[3] ExcepHandler2??浠ヤ?璇鋒?瀵矽薄涓?????avax.servlet.error.exception??javax.servlet.error.request_uri灞???