重温模糊知识点之继承httpServlet为什么不重写service方法
来源:互联网 发布:ps4应用程序数据损坏 编辑:程序博客网 时间:2024/05/01 05:13
当写一般的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的原码:
/** * 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方法。
0 0
- 重温模糊知识点之继承httpServlet为什么不重写service方法
- Servlet继承HttpServlet重写service方法的实现!
- 为什么不应该重写service方法?
- 为什么不应该重写service方法?
- 为什么不应该重写service方法?
- 【转】为什么不应该重写service方法?
- 为什么不应该重写service方法?
- 重温模糊知识点之枚举举例
- 重温模糊知识点之数据库的查询
- HttpServlet为什么有两个service方法
- 在继承HttpServlet时,为什么只重写doGet和doPost
- HttpServlet中的service方法
- HttpServlet中的service方法
- 重温那些模糊的知识点 ----Session,Cookie
- 为什么eclipse不识别HttpServlet
- 继承重写等知识点
- 继承、重写知识点汇总---
- servlet中的service()方法重写与不重写
- 蚂蚁觅食
- 又搭载8核MTK!魅族或推机海战术应战4G
- kgdb-调试内核和模块-靠谱
- getevent & setevent
- 编辑记者证练习试题
- 重温模糊知识点之继承httpServlet为什么不重写service方法
- 理科生的解放路口时京东方见识到了风纪扣
- 《木兰诗》
- MYQL字符串操作实例
- Cocos2dx 3.0 又一种创建项目方式
- 选择排序本质:内层循环找出最小值并与数组左边的交换,外层循环进行数组从左到右排序,从小到大排序。
- 史莱克到家了私家车四度假村
- git与github在ubuntu下的使用
- Linux chmod授权命令说明