优快云话题挑战赛第2期
参赛话题:学习笔记

Servlet继承关系介绍

一、javax.servlet.Servlet接口
介绍
javax.servlet.Servlet类是一个接口,我们可以来看一下Servler接口的源码:
package javax.servlet;
import java.io.IOException;
public interface Servlet {
void init(ServletConfig var1) throws ServletException;
ServletConfig getServletConfig();
void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
String getServletInfo();
void destroy();
}
…
接口中的方法都是没有代码快的抽象方法,而
javax.servlet.Servlet接口中比较核心的抽象方法有:
-
void init(); -
void service(); -
void destroy();
二、javax.servlet.GenericServlet抽象类
javax.servlet.GenericServlet实现了接口Servlet,也重写了其中的方法,但为什么是抽象类呢?道理很简单,我们来看一下源码就不难理解原因了:
源码片段:
javax.servlet.GenericServlet实现了接口Servlet:
public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
}
…
void destroy()方法被重写,但实际上并没有增添任何内容及功能。
public void destroy() {
}
…
void init()方法被重写,这是Servlet给请求初始化时用到的方法
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}
…
void service()方法被重写,但是service方法依旧是抽象方法,不存在代码块,代码块中存在这样的抽象方法,所以javax.servlet.GenericServlet是实现了Servlet接口的抽象类。
虽然service()方法依旧是没有被实现,传入的有两个参数,一个是请求对象:ServletRequest;一个是响应对象:ServletResponse。
public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
三、javax.servlet.http.HttpServlet抽象子类
javax.servlet.http.HttpServlet继承了javax.servlet.GenericServlet抽象类,因而是其抽象子类,而在HttpServlet中,实现了service()抽象方法,让我们通过源码,看一下其功能吧:
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getMethod();
long lastModified;
if (method.equals("GET")) {
lastModified = this.getLastModified(req);
if (lastModified == -1L) {
this.doGet(req, resp);
} else {
long ifModifiedSince;
try {
ifModifiedSince = req.getDateHeader("If-Modified-Since");
} catch (IllegalArgumentException var9) {
ifModifiedSince = -1L;
}
if (ifModifiedSince < lastModified / 1000L * 1000L) {
this.maybeSetLastModified(resp, lastModified);
this.doGet(req, resp);
} else {
resp.setStatus(304);
}
}
} else if (method.equals("HEAD")) {
lastModified = this.getLastModified(req);
this.maybeSetLastModified(resp, lastModified);
this.doHead(req, resp);
} else if (method.equals("POST")) {
this.doPost(req, resp);
} else if (method.equals("PUT")) {
this.doPut(req, resp);
} else if (method.equals("DELETE")) {
this.doDelete(req, resp);
} else if (method.equals("OPTIONS")) {
this.doOptions(req, resp);
} else if (method.equals("TRACE")) {
this.doTrace(req, resp);
} else {
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[]{method};
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
…
service()方法首先会通过请求对象调用getMethod()方法来获取请求方式;
获取到请求方式后,就是各种判断语句,方法会根据不同的情况,调用与其对应的do方法,而各种do方法的实现内容基本一致,我们以请求方式为post时会调用的doPost()为例子展示一下:
doPost():
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String msg = lStrings.getString("http.method_post_not_supported");
this.sendMethodNotAllowed(req, resp, msg);
}
private void sendMethodNotAllowed(HttpServletRequest req, HttpServletResponse resp, String msg) throws IOException {
String protocol = req.getProtocol();
if (protocol.length() != 0 && !protocol.endsWith("0.9") && !protocol.endsWith("1.0")) {
resp.sendError(405, msg);
} else {
resp.sendError(400, msg);
}
}
当我们的请求方式与调用的do方法不对应时,会报出405错误,这代表请求方式不支持。也就是说,在我们需要给服务器发起请求时,需要重写对应的do方法,不然就会出现错误。
总结
- 继承关系:Servlet -> GenericServlet -> HttpServlet
- Servlet中的核心方法 : init(); service(); destroy();
- 当有请求时,service方法会自动相应(实际是tomcat容器调用的)
- 在HttpServlet中会分析请求的方式、然后调用对应do方法。
- 我们在新建Servlet时,需要先考虑请求方式,从而决定重写哪个do方法

本文介绍了Servlet的基本概念及其继承关系,从Servlet接口开始,经过GenericServlet抽象类,最终到达HttpServlet抽象子类。详细解析了各部分的核心方法和服务流程。
966





