JavaWeb - Servlet原理 + ServletContext应用
Servlet原理
前提:自定义的servlet (MyServlet)继承HttpServlet,HttpServlet->GenericServlet->Servlet. 但MyServlet只重写了doGet, doPost 方法,未重写service()
1-浏览器 发送HTTP请求request 给web容器(tomcat)
2-tomcat 把 浏览器发送的request,以及一个空白的response 扔给 MyServlet的service()方法 处理 -- 通过<servlet-mapping>找到MyServlet
3-去找MyServlet.service(),但没找到 --> 再去找其父类HttpServlet,找到a) HttpServlet.service(ServletRequest...) ,然后是b) HttpServlet.service(HttpServletRequest...) 。发现会调用doGet, doPost 方法,而MyServlet重写了doGet, doPost 方法,就会调用MyServlet重写的逻辑。
//a) HttpServlet 实现 Servlet接口的service() public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { if (req instanceof HttpServletRequest && res instanceof HttpServletResponse) { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)res; //调用HttpServlet内部自己的service()方法。注意参数类型的变化。 this.service(request, response); } else { throw new ServletException("non-HTTP request or response"); } } //b) HttpServlet部自己的service()方法 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //通过request拿到method类型 String method = req.getMethod(); long lastModified; //通过method类型,来选择doGet, doPost, doXXX.... if (method.equals("GET")) { lastModified = this.getLastModified(req); if (lastModified == -1L) { this.doGet(req, resp);//调用MyServlet的doGet() } else { long ifModifiedSince = req.getDateHeader("If-Modified-Since"); if (ifModifiedSince < lastModified) { 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);//调用MyServlet的doPost() } 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); } }
ServletContext
共享数据
将来会用session或者header去做,少用ServletContext!!!
转发forward vs 重定向redirect
转发更常见的是用Request去做!!!
读取配置文件
这里的第一个斜杠“/”,不能省略。代表着web项目的根目录。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?