您的位置:首页 > 理论基础 > 计算机网络

重温模糊知识点之继承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的原码:
/**
* 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方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javaweb
相关文章推荐