ServletContext概述

2020-12-11  本文已影响0人  帅气十里不如你
@WebServlet("/servletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
        //1.通过request.getServletContext获取
        ServletContext sc1 = request.getServletContext();
        //2.通过this.ServletContext获取
        ServletContext sc2 = request.getServletContext();

        System.out.println(sc1);
        System.out.println(sc2);

        System.out.println(sc1 == sc2);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        this.doPost(request,response);
    }
}

打印结果:
org.apache.catalina.core.ApplicationContextFacade@1dcdd11
org.apache.catalina.core.ApplicationContextFacade@1dcdd11
true

@WebServlet("/servletContextDemo2")
public class ServletContextDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
        //1.获取ServletContext对象
        ServletContext servletContext = this.getServletContext();
        //2.定义文件名
        String filename = "a.jpg";
        //3.获取MIME类型
        String mine = servletContext.getMimeType(filename);
        System.out.println(mine);//输出结果image/jpeg
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
        this.doPost(request,response);
    }
}

代码实现:

@WebServlet("/servletContextDemo3")
public class ServletContextDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        //1.获取ServletContext对象
        ServletContext servletContext = this.getServletContext();
        servletContext.setAttribute("msg","sssmmmm");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        this.doPost(request,response);
    }
}
@WebServlet("/servletContextDemo4")
public class ServletContextDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        //1.获取ServletContext对象
        ServletContext servletContext = this.getServletContext();
        //获取数据
        Object msg = servletContext.getAttribute("msg");//注意获取的是Object对象
        System.out.println(msg);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        this.doPost(request,response);
    }
}

@WebServlet("/servletContextDemo5")
public class ServletContextDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        //1.获取ServletContext对象
        ServletContext servletContext = this.getServletContext();
        //获取数据
        String aPath = servletContext.getRealPath("/a.txt");
        System.out.println(aPath);

        String bPath = servletContext.getRealPath("/WEB-INF/b.txt");
        System.out.println(bPath);

        String cPath = servletContext.getRealPath("/WEB-INF/classes/c.txt");
        System.out.println(cPath);

        /**
         * 三个输出的打印结果
         * E:\Computer\Tomcat\day15\out\artifacts\day15_war_exploded\a.txt
         * E:\Computer\Tomcat\day15\out\artifacts\day15_war_exploded\WEB-INF\b.txt
         * E:\Computer\Tomcat\day15\out\artifacts\day15_war_exploded\WEB-INF\classes\c.txt
         * 注意:打印的不是工作空间而是,打印的是真实路径,也就是服务器tomcat路径
         */
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
        this.doPost(request,response);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读