重温模糊知识点之继承httpServlet为什么不重写service方法
2014-04-12 16:54
423 查看
当写一般的servlet的时候,我们编写一个servlet 去继承 GenericServlet ,那么我们就得覆盖它的一个抽象方法: abstract service(ServletRequest,ServletResponse)这个抽象方法。
存在这个一个继承关系:
interface Servlet
|---- abstract GenericServlet
|---abstract HttpServlet
我们在说一说Servlet的生命周期:init-----》service----》destory
1.默认情况下,Servlet是在第一次被访问的时候调用它的init()方法进行初始化的,执行Service方法 后,调用destory()方法消亡,生命周期完结。
2.可以进行配置,使得Servlet在容器(tomcat)加载的时候,进行初始化,在web.xml中进行下面的配置:
<servlet>
<servlet-name>Servlet名字</servlet-name>
<servlet-class>servlet完整类名</servlet-class>
<load-on-startup>1(如果初始化的servlet多的话,这里配置先后次序)</load-on-startup>
</servlet>
当写一般的servlet的时候,我们编写一个servlet 去继承 GenericServlet ,那么我们就得覆盖它的一个抽象方法: abstract service(ServletRequest,ServletResponse)这个抽象方法,执行业务,但是为什么在写有关Http协议的Servlet的时候,我们没有重写service方法?
下面是HttpServlet的原码:
通过对原码的阅读我们发现,其实HttpServlet已经实现了GenericServlet类的service方法,并且对提交方式(get,post......)进行了一些列的判断,执行相应的do....()方法.我们看一下HttpServlet中doget dopost方法的实现:
通过阅读dopost和doget的原码,我们可以看到,doget和dopost都有默认的实现,但是这个实现是“通知实现类去重写 该方法的提示信息”----也就是说,我们必须在HttpServlet的实现类里实现doget和dopost方法,并且,不能去重写service(httpServletRequest,HttpServletResponse)这个方法(实现后,就不能执行do....()方法了),所以不建议并且不能实现service方法。
存在这个一个继承关系:
interface Servlet
|---- abstract GenericServlet
|---abstract HttpServlet
我们在说一说Servlet的生命周期:init-----》service----》destory
1.默认情况下,Servlet是在第一次被访问的时候调用它的init()方法进行初始化的,执行Service方法 后,调用destory()方法消亡,生命周期完结。
2.可以进行配置,使得Servlet在容器(tomcat)加载的时候,进行初始化,在web.xml中进行下面的配置:
<servlet>
<servlet-name>Servlet名字</servlet-name>
<servlet-class>servlet完整类名</servlet-class>
<load-on-startup>1(如果初始化的servlet多的话,这里配置先后次序)</load-on-startup>
</servlet>
当写一般的servlet的时候,我们编写一个servlet 去继承 GenericServlet ,那么我们就得覆盖它的一个抽象方法: abstract service(ServletRequest,ServletResponse)这个抽象方法,执行业务,但是为什么在写有关Http协议的Servlet的时候,我们没有重写service方法?
下面是HttpServlet的原码:
/** * Receives standard HTTP requests from the public * <code>service</code> method and dispatches * them to the <code>do</code><i>XXX</i> methods defined in * this class. This method is an HTTP-specific version of the * {@link javax.servlet.Servlet#service} method. There's no * need to override this method. * * @param req the {@link HttpServletRequest} object that * contains the request the client made of * the servlet * * @param resp the {@link HttpServletResponse} object that * contains the response the servlet returns * to the client * * @exception IOException if an input or output error occurs * while the servlet is handling the * HTTP request * * @exception ServletException if the HTTP request * cannot be handled * * @see javax.servlet.Servlet#service */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod(); if (method.equals(METHOD_GET)) { long lastModified = getLastModified(req); if (lastModified == -1) { // servlet doesn't support if-modified-since, no reason // to go through further expensive logic doGet(req, resp); } else { long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); if (ifModifiedSince < (lastModified / 1000 * 1000)) { // If the servlet mod time is later, call doGet() // Round down to the nearest second for a proper compare // A ifModifiedSince of -1 will always be less maybeSetLastModified(resp, lastModified); doGet(req, resp); } else { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } } } else if (method.equals(METHOD_HEAD)) { long lastModified = getLastModified(req); maybeSetLastModified(resp, lastModified); doHead(req, resp); } else if (method.equals(METHOD_POST)) { doPost(req, resp); } else if (method.equals(METHOD_PUT)) { doPut(req, resp); } else if (method.equals(METHOD_DELETE)) { doDelete(req, resp); } else if (method.equals(METHOD_OPTIONS)) { doOptions(req,resp); } else if (method.equals(METHOD_TRACE)) { doTrace(req,resp); } else { // // Note that this means NO servlet supports whatever // method was requested, anywhere on this server. // String errMsg = lStrings.getString("http.method_not_implemented"); Object[] errArgs = new Object[1]; errArgs[0] = method; errMsg = MessageFormat.format(errMsg, errArgs); resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg); } }
通过对原码的阅读我们发现,其实HttpServlet已经实现了GenericServlet类的service方法,并且对提交方式(get,post......)进行了一些列的判断,执行相应的do....()方法.我们看一下HttpServlet中doget dopost方法的实现:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String protocol = req.getProtocol(); String msg = lStrings.getString("http.method_get_not_supported"); if (protocol.endsWith("1.1")) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); } else { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); } } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String protocol = req.getProtocol(); String msg = lStrings.getString("http.method_post_not_supported"); if (protocol.endsWith("1.1")) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); } else { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); } }
通过阅读dopost和doget的原码,我们可以看到,doget和dopost都有默认的实现,但是这个实现是“通知实现类去重写 该方法的提示信息”----也就是说,我们必须在HttpServlet的实现类里实现doget和dopost方法,并且,不能去重写service(httpServletRequest,HttpServletResponse)这个方法(实现后,就不能执行do....()方法了),所以不建议并且不能实现service方法。
相关文章推荐
- 为什么不应该重写service方法?
- 在继承HttpServlet时,为什么只重写doGet和doPost
- 继承到底是什么,为什么private static的方法不能被重写
- 为什么不应该重写service方法?
- HttpServlet为什么有两个service方法
- myeclipse实现Servlet实例(2) 继承GenericServet类实现,需要重写service方法
- 1.为什么需要继承?如何继承?继承到什么? 2.方法的重载与重写的区别 3.类型的自动转换和强制转换 4.子类特有的方法的调用规则 5.子类重写的方法的调用规则 6.多态的理解 7.面向对象的三大特征
- 为什么不应该重写service方法?
- 继承之虚方法和重写方法知识点和思考练习
- Servlet继承HttpServlet重写service方法的实现!
- 为什么不应该重写service方法?
- 【转】为什么不应该重写service方法?
- readOnly、const这两者的区别及继承中为什么父类中的方法被重写后之后系统做了什么事?
- 为什么要重写View的onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法
- 面向对象,类的组合关系,继承,实现,方法重写,方法重载,this的使用,抽象方法和抽象类的比较,父类构造方法存在的意义,多态的是用和解析,各种访问修饰符
- 继承CCSprite时不要重写init方法,否则会无限循环
- 牛客网Java刷题知识点之什么是内部类、为什么要使用内部类、内部类如何使用外部类的属性和方法、成员内部类、局部内部类、静态内部类、匿名内部类
- 关于重写servlet的service()方法时调用父类service方法的问题
- 为什么要重写hashcode() 方法
- 继承概念与方法的重写小结