• ServletContext对象


    一、ServletContext对象的引入

    ServletContext对象,叫做Servlet的上下文对象。表示一个当前的web应用环境。一个web应用只有一个Servlet对象。

    二、Servlet对象的创建和获得

    ServletContext的生命周期

      生:服务器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。销毁:服务器关闭或者当前应用删除。

    二、ServletContext对象的创建:

    public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //得到ServletContext的方式1:通过ServletConfig对象的getServletContext()的方法
            ServletContext context=this.getServletConfig().getServletContext();
            //得到ServletContext的方式2
            context=this.getServletContext();
        
        } 

    三、ServletContext的作用

    1.多个Servlet通过ServletContext对象实现数据共享。

      由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。

    2.获取WEB应用的初始化参数,类似于ServletConfig。

    <context-param>   
    
      <param-name>url</param-name>   
    
      <param-value>jdbc:mysql://localhost:3306/ordername</param-value>   
    
     </context-param>   

    3、实现Servlet的转发:

    //Servlet的转发
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String date="ServletContext";
            //不能通过context,要通过request域
            this.getServletContext().setAttribute("date", date);
            RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/demo.jsp");
            rd.forward(request, response);    
        }

    转发给 demo.jsp

      <body>
      <h1>
          <font color="red">
        <%
            String date=(String)application.getAttribute("date");
            out.write(date);
         %>
         </font>
         </h1>
      </body>

    4.利用ServletContext对象读取资源文件。

    db.properties

    url = "jdbc:mysql://localhost:3306/test"
    username="root"
    name="root"

     在实际开发中,用作资源文件的文件类型,通常是:xml、properties.读取XML文件需要对文件进行解析。今天我们学习对properties文件进行读取的方法。在web工程中,我们一般来说,是不能采用传统方式读取配置文件的,因为读取写文件的地址相对的是jvm的启动目录(tomcat的bin目录),所以我们要使用web绝对目录来获取配置文件的地址。 

    第一种:使用ServletContext的getResourceAsStream方法:返回资源文件的读取字节流

      public  void test1() throws IOException{
            InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
            Properties props=new Properties();
            props.load(in);
            String url=props.getProperty("url");
            String username=props.getProperty("username");
            String password=props.getProperty("password");
            
            System.out.println(url);
            System.out.println(username);
            System.out.println(password);
        }

    第二种:使用ServletContext的getRealPath方法,获得文件的完整绝对路径path,再使用字节流读取path下的文件

        public void test2() throws IOException{
            String path =this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
            FileInputStream in=new FileInputStream(path);
         ////相比第一种方法的好处是:除了可以获取数据,还可以获取资源文件的名称 String filename
    =path.substring(path.lastIndexOf("\")+1); System.out.println(filename); Properties props=new Properties(); props.load(in); String url=props.getProperty("url"); String username=props.getProperty("username"); String password=props.getProperty("password"); System.out.println(url); System.out.println(username); System.out.println(password); }

     Web工程中,不同的资源文件的读取方式:

    A.当资源文件在包下面时,即在src下边建立的包下边:src/cn.lyjs.context/db.properties

    InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/lyjs/context/db.properties");

    B.资源文件在web-inf下

     in = this.getServletContext().getResourceAsStream("/WEB-INF/db.properties");

    C.资源文件在web工程中

    in = this.getServletContext().getResourceAsStream("/db.properties");

     5、在非servlet程序中如何读取配置文件:用类装载器

    public class Userdao {
        //如果读取资源文件的程序不是Servlet的话,我们只能通过加载器来读
        public void update() throws IOException{
            InputStream in=Userdao.class.getClassLoader().getResourceAsStream("db.properties");
            Properties prop=new Properties();
            prop.load(in);
            System.out.println(prop.getProperty("url"));
        }
    }

     缺点:在线程休眠过程中,即使改动了资源文件,获取到的还是原始内容,类加载器只加载一次

    解决方法:使用绝对地址来访问

        public void update2() throws IOException{
            String path=Userdao.class.getClassLoader().getResource("db.properties").getPath();
            FileInputStream in=new FileInputStream(path);
            Properties prop=new Properties();
            prop.load(in);
            System.out.println(prop.getProperty("url"));
        }

    注意:用类装载器读取资源文件时,千万要注意,资源文件绝对不能太大,否则极易导致内存溢出

  • 相关阅读:
    制作透明色:《CSS3 RGBA》与Opacity样式用法
    制作动画效果:《CSS3 Animation》
    CISCO运维记录之3650堆叠设备升级IOS(Version 16.3.6版本存在bug)
    08 Zabbix4.0系统配置事件通知
    24 Zabbix系统配置日志监控告警--关键字触发
    15 Zabbix Item类型之Zabbix trapper类型
    06 Zabbix4.0系统CISCO交换机告警模板规划信息(基础)
    05 Zabbix4.0触发器表达式Trigger expression支持的函数
    04 Zabbix4.0系统配置触发器trigger
    03 Zabbix4.0添加cisco交换机基本监控步骤
  • 原文地址:https://www.cnblogs.com/lyjs/p/4869930.html
Copyright © 2020-2023  润新知