• ServletContext对象


    1.什么是ServletContext对象?

    ServletContext官方叫servlet上下文。服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象。这个对象全局唯一,而且工程内部的所有servlet都共享这个对象。所以叫全局应用程序共享对象。

    2.如果创建ServletContext对象?

    (1)Request对象调用getServletContext()方法。

    (2)HttpServlet对象调用getServletContext()方法。

    示例:

    public class ServletDemo1 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context1 = req.getServletContext();
            ServletContext context2 = this.getServletContext();
            System.out.println(context1 == context2);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
        }
    }
    
    // 输出结果:
    true

     

    3.ServletContext对象有什么用?

    (1)获取MIME类型    String getMimeType(String file)

    什么是MIME类型?

    MIME类型:在互联网通信过程中定义的一种文件数据类型。

    格式: 大类型/小类型   text/html        image/jpeg

    (2)域对象:共享数据(范围:所有用户所有请求的数据)

    1. setAttribute(String name,Object value)
    2. getAttribute(String name)
    3. removeAttribute(String name)

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

    说到服务器目录,就先要说一下我们的本地工作目录。

    当我们打开IDEA的时候,工作目录是这样的:

    但是Tomcat并不是根据这些文件运行的。他根据的是~/桌面/JavaTemp/Demo2/out/artifacts/Demo2_war_exploded/这个目录的文件去运行的!同时这个目录也对应着项目URL的虚拟目录!

    这个目录下的文件路径如下:

    Demo2_war_exploded/
    ├── c.txt
    ├── index.jsp
    └── WEB-INF

        ├── libs
        ├── b.txt
        └── classes
            ├── a.txt
            └── com
                └── chichung
                    └── web
                        └── ServletDemo1.class
    所以在指定文件目录时要按照服务器的路径来指定,不能根据我们的工作目录来指定!

    ServletContext对象可以获取文件的真实路径,也就是服务器的路径。

    方法:String getRealPath(String path)

    示例:

    @WebServlet("/servletdemo1")
    public class ServletDemo1 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context1 = req.getServletContext();
    
            String c = context1.getRealPath("/c.txt");
            System.out.println(c);
    
            String b = context1.getRealPath("/WEB-INF/b.txt");
            System.out.println(b);
    
            String a = context1.getRealPath("/WEB-INF/classes/a.txt");
            System.out.println(a);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
        }
    }
    
    // 输出结果:
    /home/chichung/桌面/JavaTemp/Demo2/out/artifacts/Demo2_war_exploded/c.txt
    /home/chichung/桌面/JavaTemp/Demo2/out/artifacts/Demo2_war_exploded/WEB-INF/b.txt
    /home/chichung/桌面/JavaTemp/Demo2/out/artifacts/Demo2_war_exploded/WEB-INF/classes/a.txt

    4.可以获取当前工程的虚拟路径(最重要!!!)

    String  getContextPath()

     在我们写路径时,虚拟路径部分用这个替代,如果以后更改虚拟路径就不用一个一个来替换了!

  • 相关阅读:
    使用ACEGI搭建权限系统:第三部分
    分支在版本树中的应用(使用subversion)
    acegi安全框架使用:第二部分
    错误数据导致java.lang.IllegalArgumentException:Unsupported configuration attributes
    移动中间件和wap网关的比较
    3年后,又回到了.net阵营
    android中listView的几点总结
    oracle相关分布式数据解决方案
    ajax实现用户名存在校验
    使用template method模式简化android列表页面
  • 原文地址:https://www.cnblogs.com/chichung/p/10322450.html
Copyright © 2020-2023  润新知