JavaWeb - Servlet原理 + ServletContext应用

回到顶部(go to top)

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);
        }

    }        
复制代码

 

 

 

 

 

 

回到顶部(go to top)

ServletContext

共享数据

将来会用session或者header去做,少用ServletContext!!!

 

 

 

 

 

转发forward vs 重定向redirect

转发更常见的是用Request去做!!!

 

 

 

 

读取配置文件

这里的第一个斜杠“/”,不能省略。代表着web项目的根目录。

 

posted on   frank_cui  阅读(105)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

levels of contents
点击右上角即可分享
微信分享提示