ServletContext_概述与ServletContext_获取

ServletContext_概述

      ServletContext对象

          1.概念:代表整个web应用,可以和程序的容器(服务器)类通信  

          2.功能:

             1.获取MIME类型:

             2.域对象:共享数据  

             3.获取文件的真实(服务器)路径   

 

 

ServletContext_获取 

.    

 ServletContext对象

          1.概念:代表整个web应用,可以和程序的容器(服务器)类通信  

          2.获取:

             1通过Request对象获取

              request.getServletContext();

             2.通过HttpSevlert获取    

              this.getServletContext();

package qh.xueqiang.servletcontext;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/contextDemo01")
public class ContextDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*

                    ServletContext_获取
                     1通过Request对象获取
              request.getServletContext();
             2.通过HttpSevlert获取
              this.getServletContext();
         */

            //1.通过Request对象获取
        ServletContext context1 = request.getServletContext();
            //2.通过HttpSevlert获取
        ServletContext context2 = this.getServletContext();

        System.out.println(context1);
        System.out.println(context2);

        System.out.println(context1 == context2);

    }
}

 

       

 

 

 

          3.功能:

             1.获取MIME类型:

             2.域对象:共享数据  

             3.获取文件的真实(服务器)路径   

posted @ 2023-02-02 16:43  zj勇敢飞,xx永相随  阅读(20)  评论(0编辑  收藏  举报