Web中各个位置的配置文件的读取

开发中有些都是痛啦,都是经验的积累才能变成现在这种老成的样子
今天介绍的是Web中各个位置的配置文件或者资源文件的读取

先看一下我把那些配置文件放在Web资源的哪些位置 


package cn.itcast.servlet;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//其他读取资源文件的方式
public class ServletDemo7 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test7();
       
    }
    //利用ServletContext读取a1.properties
    private void test1() throws IOException, FileNotFoundException {
        ServletContext sc = getServletContext();
        String path = sc.getRealPath("/WEB-INF/a1.properties");
        Properties props = new Properties();
        props.load(new FileInputStream(path));
        String value = props.getProperty("username");
        System.out.println(value);
    }
    //利用ServletContext读取a2.properties
    private void test2() throws IOException, FileNotFoundException {
        ServletContext sc = getServletContext();
        String path = sc.getRealPath("/WEB-INF/classes/a2.properties");
        Properties props = new Properties();
        props.load(new FileInputStream(path));
        String value = props.getProperty("username");
        System.out.println(value);
    }
    //利用ServletContext读取a3.properties
    private void test3() throws IOException, FileNotFoundException {
        ServletContext sc = getServletContext();
        String path = sc.getRealPath("/WEB-INF/classes/cn/itcast/resources/a3.properties");
        Properties props = new Properties();
        props.load(new FileInputStream(path));
        String value = props.getProperty("username");
        System.out.println(value);
    }
    //------------------------------------------------------------------
    //利用ResourceBundle读取配置文件a2.properties
    private void test4() throws IOException, FileNotFoundException {
        //ResourceBundle是Java自带的一个方法,不能new出来,只能调用它的内部静态方法
        ResourceBundle rb = ResourceBundle.getBundle("a2");//基名(也就说扩展名前面那个名字  E.a3.properties  基名也就是a3)
        String value = rb.getString("username");
        System.out.println(value);
    }
    //利用ResourceBundle读取配置文件a3.properties
    private void test5() throws IOException, FileNotFoundException {
        ResourceBundle rb = ResourceBundle.getBundle("cn.itcast.resources.a3");//基名
        String value = rb.getString("username");
        System.out.println(value);
    }
    //-----------------------------------------------------------------------------------
   
    /*
         接下来,利用类加载器读取配置文件a2.properties
         也就是调用ClassLoader
         下面是最基础的使用,可是这里有一个缺点,那就是如果你在程序发布以后,如果修改了properties配置文件,它读取时不改变数据的
         这也是网络上经常有人会问,因为如果你知道Java运行机制的话,就知道数据一旦编译之后就不会改变
        
         所以,我会在test7()里面做点小小的改动就修改了这个Bug
         顺便说一声,如果使用ServletContext去获取文件路径的话,就不会产生这个Bug,但是,ServletContext只能在Servlet中使用
    如果你是在Dao中想获取配置文件的信息呢,虽然可以把ServletContext的值去传递,但是在开发中不会去使用,因为你WEB层嵌入到了Dao层,数据不安全
        
     */
    private void test6() throws IOException, FileNotFoundException {
        ClassLoader cl = ServletDemo7.class.getClassLoader();//得到ServletDemo7的类加载器
        InputStream in = cl.getResourceAsStream("a2.properties");
        Properties props = new Properties();
        props.load(in);
        String value = props.getProperty("username");
        System.out.println(value);
    }
    /*
         test7就是为了解决test6的问题才弄出来
         比如你一个Dao想访问本地的配置文件,可是因为不是ServletContext,那你就不能调用那些获取路径的方法
         这时候我们应该怎么办
         还有一个问题也摆在面前:那就是如果你在程序发布以后,如果修改了properties配置文件,它读取时不改变数据的,这又该怎么解决
         解决方法是:我们通过类加载器获取文件路径,然后用传统方法去读取文件
     */
    private void test7() throws IOException, FileNotFoundException {
        ClassLoader cl = ServletDemo7.class.getClassLoader();//得到ServletDemo7的类加载器
        /*
            InputStream in = cl.getResourceAsStream("a2.properties");
           
            把这个换掉
           
        */
        URL url=cl.getResource("a2.properties");
        InputStream in=url.openStream();
        Properties props = new Properties();
        props.load(in);
        String value = props.getProperty("username");
        System.out.println(value);
    }
   
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值