public abstract class HttpServlet extends GenericServlet
package com.xh.test.api; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Created by root on 17-11-12. */ public class MyHttpServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.service(req, resp); System.out.println(">>>MyHttpServlet.service"); resp.getWriter().write("MyHttpServlet.service"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //super.doGet(req, resp); System.out.println(">>>MyHttpServlet.doGet"); resp.getWriter().write("MyHttpServlet.doGet"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //super.doPost(req, resp); System.out.println(">>>MyHttpServlet.doPost"); resp.getWriter().write("MyHttpServlet.doPost"); } }
在restclient插件输入:http://localhost:8080/testservlet/1?id=100 ,切换方法GET,POST
输出是:
MyHttpServlet.doGetMyHttpServlet.service
MyHttpServlet.doPostMyHttpServlet.service
控制台:
>>>MyHttpServlet.doGet
>>>MyHttpServlet.service
>>>MyHttpServlet.doPost
>>>MyHttpServlet.service
PS:浏览器的请求过来,都要经过Servlet的service,如果自己不实现则走HttpServlet的。如果自己实现的,没有调用super.service(req, resp),
则直接返回,如果调用了super.service(req, resp),则HttpServlet会找当前类的对应的方法,如doGet,doPost,如果没有对应的实现,则抛异常,
如果实现了,就执行当前类的doGet,doPost方法,执行完后又返回到service方法,执行完返回给浏览器。
所以我们一般二者选其一实现(当然建议是doGet,doPost)。
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) { // 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); } }
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); } }