zoukankan      html  css  js  c++  java
  • ServeletContext对象的获取及其作用

    官方对ServeletContext的解释是定义了一组可以使servelet和web容器通信的方法。

    也就是servelet对象可以通过serveletcontext对象与web容器进行交互。

    接下来讲解ServeletContext的获取及其作用

    • 获取:(两种方法)

      1. 通过request对象获取:request.getServletContext();
      2. 通过当前类获取:this.getServletContext();

    注意:无论通过哪种方式我们获得的ServeletContext对象是同一个对象。

    • 作用:

      1. 获取文件对应的MIME类型
      2. 获取文件的真实路径(真实路径是部署在服务器上的文件路径,也就是真实访问的路径)
      3. 在整个web项目中共享数据

    演示:

    1. 获取文件对应的MIME类型:

    代码:

    @WebServlet("/servlet01")
    public class Servlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取ServeletContext对象
            ServletContext context = this.getServletContext();
            String s="a.txt";
            //获取对应的MIME类型
            String mimeType = context.getMimeType(s);
            System.out.println(mimeType);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }
    }

    结果:

        2.获取文件的真实路径(真实路径是部署在服务器上的文件路径,也就是真实访问的路径)

    • 首先我们先分别在src,web,WEB-INF下分别创建a.txt,b.txt,c.txt文件

     然后获得它们部署在服务器中的真实路径:

    代码:

    @WebServlet("/servlet01")
    public class Servlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取ServeletContext对象
            ServletContext context = this.getServletContext();
        //获取b.txt文件的真实路径:
            String brealPath = context.getRealPath("/b.txt");
            System.out.println("b.txt的真实路径是:"+brealPath);
            //获取c.txt文件的真实路径:
            String crealPath = context.getRealPath("/WEB-INF/c.txt");
            System.out.println("c.txt的真实路径是:"+crealPath);
            //获取a.txt文件的真实路径:
            String arealPath = context.getRealPath("/WEB-INF/classes/a.txt");
            System.out.println("a.txt的真实路径是:"+arealPath);
    
    
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }
    }

    结果:

    注意:

    src下的文件在项目中部署的真实路径在WEB-INF下的classes文件下(src相当于classes)

        3.在整个web项目中共享数据

    ServeletContext对象的作用域是整个项目:

    代码:

    @WebServlet("/servlet01")
    public class Servlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取ServeletContext对象
            ServletContext context = this.getServletContext();
            context.setAttribute("name", "zhangsan");
    
    
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }
    }

    在资源类2中打开另一个浏览器模拟另一个用户获取:

    @WebServlet("/servlet02")
    public class Servlet02 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String name = (String) request.getServletContext().getAttribute("name");//在另一个资源类中获取
            System.out.println(name);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }
    }

    结果:



      

    迎风少年
  • 相关阅读:
    (winform cookie)如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证
    NBear官方MSN群
    NBear 支持基于操作符重载的强类型Where及OrderBy查询
    NBear视频 0.1 NBear类库结构及功能简介[发布时间:9/5]
    NBear 使用Entity Configurator设置实体元数据、生成数据库创建脚本
    ORM中的继承关系映射全解——单表继承体系、一实体一具体表、一实体一扩展表、接口映射
    实例解析继承体系重构及ORM映射
    NBearV2.1.0,新增Oracle和MySql Db Provider,诚征测试
    NBear视频 1.1 实体定义基础[发布时间:9/5]
    使用主动实体(ActiveEntity)简化继承体系映射类的写操作
  • 原文地址:https://www.cnblogs.com/ZYH-coder0927/p/13671735.html
Copyright © 2011-2022 走看看