重新学习Servlet二
1 | public abstract class HttpServlet extends GenericServlet |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 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); } } |
1 2 3 4 5 6 7 8 9 10 11 | 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); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步