博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet生命周期中的service方法分析
阅读量:5319 次
发布时间:2019-06-14

本文共 5127 字,大约阅读时间需要 17 分钟。

问题

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 }

 

转载于:https://www.cnblogs.com/ICE_Inspire/p/5150170.html

你可能感兴趣的文章
POJ 1015 Jury Compromise(双塔dp)
查看>>
论三星输入法的好坏
查看>>
Linux 终端连接工具 XShell v6.0.01 企业便携版
查看>>
JS写一个简单日历
查看>>
Python 发 邮件
查看>>
mysql忘记密码的解决办法
查看>>
全面分析Java的垃圾回收机制2
查看>>
[Code Festival 2017 qual A] C: Palindromic Matrix
查看>>
修改博客园css样式
查看>>
Python3 高阶函数
查看>>
初始面向对象
查看>>
leetcode Letter Combinations of a Phone Number
查看>>
Unity 5.4 测试版本新特性---因吹丝停
查看>>
7.5 文件操作
查看>>
MyEclipse中将普通Java项目convert(转化)为Maven项目
查看>>
node js 安装.node-gyp/8.9.4 权限 无法访问
查看>>
windows基本命令
查看>>
VMware中CentOS设置静态IP
查看>>
[poj1006]Biorhythms
查看>>
Hyper-V虚拟机上安装一个图形界面的Linux系统
查看>>