如果一个类继承了HttpServlet,那么他的实现过程是怎样的?
我的理解:一个类继承了HttpServlet类之后,首先会去调用实现父类的 service(ServletRequest req,ServletResponse res)方法通 过 参 数 的 向 下 转 型 , 然 后 调 用 重 载的 service(HttpservletRequest,HttpServletResponse)方法,目的是减少与http协议之间产生的耦合性,然后在service方法中进行判断提交方式的Do Get 还是Do Post 或者是其他提交方式,之后再通过调用我们自己重写的do get 和do post方法执行。
public HttpServlet() {
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String msg = lStrings.getString("http.method_get_not_supported");
this.sendMethodNotAllowed(req, resp, msg);
}
protected long getLastModified(HttpServletRequest req) {
return -1L;
}
protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (DispatcherType.INCLUDE.equals(req.getDispatcherType())) {
this.doGet(req, resp);
} else {
HttpServlet.NoBodyResponse response = new HttpServlet.NoBodyResponse(resp);
this.doGet(req, response);
if (req.isAsyncStarted()) {
req.getAsyncContext().addListener(new HttpServlet.NoBodyAsyncContextListener(response));