你的位置:首页 > Java教程

[Java教程]Servlet生命周期中的service方法分析

问题
ServletLifeCycle中的service方法内,有super.service(request, response); 会执行this.doGet(HttpServletRequest request, HttpServletResponse response);
没有super.service(request, response);,则不执行this.doGet(...). 是怎么实现的?

举一反三:
一个子类,覆写的方法内,如果调用了父类的该方法,会执行子类内的另一个方法;
覆写的方法内,如果没有调用父类的该方法,就不会执行子类内的另一个方法;

分析 ----->符号是关键注释

 1 public class ServletLifeCycle extends HttpServlet { 2   private static final long serialVersionUID = 1L; 3    4   @Override 5   public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { 6     // TODO Auto-generated method stub 7     //this.doGet((HttpServletRequest)request, (HttpServletResponse)response); 8     super.service(request, response);//------------------->执行父类的service(ServletRequest request, ServletResponse response)方法 9      10     System.out.println("处理客户端请求"); 11   } 12  13   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 14     // TODO Auto-generated method stub 15     System.out.println("处理过程"); 16     response.getWriter().append("Served at: ").append(request.getContextPath()); 17   } 18  19 } 20  21  22  23  24 public abstract class HttpServlet extends GenericServlet { 25 public void service(ServletRequest req, ServletResponse res) 26   throws ServletException, IOException { 27  28   HttpServletRequest request; 29   HttpServletResponse response; 30  31   try { 32     request = (HttpServletRequest) req; 33     response = (HttpServletResponse) res; 34   } catch (ClassCastException e) { 35     throw new ServletException("non-HTTP request or response"); 36   } 37   service(request, response);//------------------->父类的方法的重载执行父类的service(HttpServletRequest request, HttpServletResponse response)方法 38   //------------------->我的理解是,如果没有重载,会出现死循环.  走到此处又执行子类ServletLifeCycle的service方法,子类又调用父类service方法,循环嵌套. 39 } 40  41  42 protected void service(HttpServletRequest req, HttpServletResponse resp) 43     throws ServletException, IOException { 44  45     String method = req.getMethod(); 46  47     if (method.equals(METHOD_GET)) { 48       long lastModified = getLastModified(req); 49       if (lastModified == -1) { 50         // servlet doesn't support if-modified-since, no reason 51         // to go through further expensive logic 52         doGet(req, resp);//------------------->调用子类ServletLifeCycle的doGet方法 53       } else { 54         long ifModifiedSince; 55         try { 56           ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); 57         } catch (IllegalArgumentException iae) { 58           // Invalid date header - proceed as if none was set 59           ifModifiedSince = -1; 60         } 61         if (ifModifiedSince < (lastModified / 1000 * 1000)) { 62           // If the servlet mod time is later, call doGet() 63           // Round down to the nearest second for a proper compare 64           // A ifModifiedSince of -1 will always be less 65           maybeSetLastModified(resp, lastModified); 66           doGet(req, resp); 67         } else { 68           resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 69         } 70       } 71  72     } else if (method.equals(METHOD_HEAD)) { 73       long lastModified = getLastModified(req); 74       maybeSetLastModified(resp, lastModified); 75       doHead(req, resp); 76  77     } else if (method.equals(METHOD_POST)) { 78       doPost(req, resp); 79  80     } else if (method.equals(METHOD_PUT)) { 81       doPut(req, resp); 82  83     } else if (method.equals(METHOD_DELETE)) { 84       doDelete(req, resp); 85  86     } else if (method.equals(METHOD_OPTIONS)) { 87       doOptions(req,resp); 88  89     } else if (method.equals(METHOD_TRACE)) { 90       doTrace(req,resp); 91  92     } else { 93       // 94       // Note that this means NO servlet supports whatever 95       // method was requested, anywhere on this server. 96       // 97  98       String errMsg = lStrings.getString("http.method_not_implemented"); 99       Object[] errArgs = new Object[1];100       errArgs[0] = method;101       errMsg = MessageFormat.format(errMsg, errArgs);102 103       resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);104     }105   }106 107 108 protected void doGet(HttpServletRequest req, HttpServletResponse resp)109   throws ServletException, IOException110 {111   String protocol = req.getProtocol();112   String msg = lStrings.getString("http.method_get_not_supported");113   if (protocol.endsWith("1.1")) {114     resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);115   } else {116     resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);117   }118 }119 }