void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;也就是说service方法是在Servlet接口中定义的。javax.servlet.GenericServlet 是一个抽象类,实现了javax.servlet.Servlet接口,这个抽象类没有给出service 的具体实现:
public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;javax.servlet.GenericServlet 有一个子类 javax.servlet.http.HttpServlet, 这个就不是一个抽象类了,它实现了 service 方法
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { HttpServletRequest request; HttpServletResponse response; try { request = (HttpServletRequest)req; response = (HttpServletResponse)res; } catch (ClassCastException var6) { throw new ServletException("non-HTTP request or response"); } this.service(request, response); }
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); } }
不管是post还是get方法提交过来的连接,都会在service中处理
doGet和doPost方法的实现是在HttpServlet类中完成的,并通过覆盖这些方法来处理对应类型的HTTP请求。
在HttpServlet类中,doGet和doPost等方法都是被定义为protected类型的,仅供子类继承并覆盖使用。这些方法的具体实现会根据请求方法的类型来执行相应的业务逻辑。
以doGet方法为例,其在HttpServlet类中的定义如下:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //do something }
在实际使用中,开发者通过继承HttpServlet类并覆盖doGet/doPost等方法,来自定义Servlet的行为逻辑。当对应的HTTP请求发生时,容器会自动调用对应的doGet/doPost方法来执行开发者定义的业务逻辑。
例如,下面的代码片段展示了一个简单的实例:
public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>MyServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); } }
这个Servlet会在接收到一个HTTP GET请求时,回复一个HTML响应,包含了一个"Hellow World!"的标题。开发者通过继承了HttpServlet,并覆盖了doGet方法,来实现对GET请求的自定义处理逻辑。当发生GET请求时,容器会自动调用doGet方法来执行程序逻辑,生成对应的HTML响应并返回给客户端。