获取资源文件的方式

public class MapApp1 extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  //获取资源的类常用的有如下三种
//其中抓门用来读取properties文件的是默认根路径是src路径
//classloader加载器的默认路径是src路径
         //资源流的默认加载路径是webapps即应用目录开始所谓的应用目录就是web.xml中的
//urlpatarn 和项目直接可以访问的webRoot目录

//即那些直接可以进行访问的资源。

//最后一层 也就是根不可以不写/ 自动追加

  ResourceBundle bundle = ResourceBundle.getBundle("/com/itheima/servlet/db");
  String str = bundle.getString("username");
  System.out.println(str);
  
  ResourceBundle bundle1 = ResourceBundle.getBundle("com/itheima/servlet/db");
  String str1 = bundle1.getString("username");
  System.out.println(str1);  
  
  ResourceBundle bundle2= ResourceBundle.getBundle("a");
  String str2 = bundle2.getString("username");
  System.out.println(str2);  
  
  
  

  InputStream systemResourceAsStream = this.getClass().getClassLoader().getResourceAsStream("db12.properties");
  Properties  pro9=new Properties();
  pro9.load(systemResourceAsStream);
  String name9 = pro9.getProperty("username");
  System.out.println(name9);
  
  
  
  
  InputStream stream = getServletContext().getResourceAsStream("/db.properties");
  Properties  pro=new Properties();
  pro.load(stream);
  String name = pro.getProperty("username");
  System.out.println(name);
  
  InputStream stream1 = getServletContext().getResourceAsStream("/WEB-INF/classes/com/itheima/servlet/db.properties");
  
  Properties  pro1=new Properties();
  pro.load(stream1);
  String name1 = pro.getProperty("username");
  System.out.println(name1);
  
          InputStream stream2 = getServletContext().getResourceAsStream("WEB-INF/classes/com/itheima/servlet/db.properties");
  Properties  pro2=new Properties();
  pro.load(stream2);
  String name2 = pro.getProperty("username");
  System.out.println(name2);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值