JAVA中ResourceBundle使用详解(一)

最近在看HttpServlet源码时候,碰到ResourceBundle的使用,就此记录一下。

一、ResourceBundle是啥

在package java.util;下

这个类主要用来解决国际化和本地化问题。国际化和本地化是啥,另一篇文章有写,这里直接上用法。

这个类获取的属性也是来自于properties属性文件,获取properties属性有两个方式,一是通过Properties;二是通过ResourceBundle方式。ResourceBundle与Properties的区别在于ResourceBundle通常是用于国际化的属性配置文件读取,Properties则是一般的属性配置文件读取。

Properties与ResourceBundle两个类都可以读取属性文件中以key/value形式存储的键值对,但是ResourceBundle读取属性文件时操作相对简单

Properties类继承Hashtable,将键值对存储在集合中。基于输入流从属性文件中读取键值对,load()方法调用完毕,就与输入流脱离关系,不会自动关闭输入流,需要手动关闭。Properties这个本文不作展开。

而ResourceBundle该类是基于类读取属性文件:他是将属性文件当作类,意味着属性文件必须放在包中,使用属性文件的全限定性类名而非路径指代属性文件,简单的说,就是将文件放在包中,通过使用类的全限定方式来指定,而不是通过指定路径的方式。如下:

/**
     * 基于类读取属性文件:该方法将属性文件当作类来处理,属性文件放在包中,使用属性文件的全限定性而非路径来指代文件
     */
    @Test
    public void demo() {
        ResourceBundle bundle = ResourceBundle.getBundle("com.four.properties.demo");
        //这里就可以直接获取该配置文件的属性了
        System.out.println("driver=" + bundle.getString("jdbc.driver"));
        System.out.println("url=" + bundle.getString("jdbc.url"));
        System.out.println("username=" + bundle.getString("jdbc.username"));
        System.out.println("password=" + bundle.getString("jdbc.password"));

        System.out.println("-----------------------------");
        System.out.println("遍历属性文件");
        Enumeration<String> keys = bundle.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            System.out.println(key + "=" + bundle.getString(key));
        }
    }

再说的简单点,就是通过key来获取properties属性配置文件的value,完了,就这样,没啥了

二、HttpServlet中的使用

HttpServlet里使用的是

private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings");

这里取得是本地的设置,这个设置来源于这个文件,注意看,他是放在包中的,也是通过类的全限定路径指定文件javax.servlet.http.LocalStrings。
在这里插入图片描述
LocalStrings属性配置文件的配置

err.cookie_name_is_token=Cookie name \"{0}\" is a reserved token
err.cookie_name_blank=Cookie name must not be null or empty
err.io.nullArray=Null passed for byte array in write method
err.io.indexOutOfBounds=Invalid offset [{0}] and / or length [{1}] specified for array of size [{2}]
err.io.short_read=Short Read
err.ise.getWriter=Illegal to call getWriter() after getOutputStream() has been called
err.ise.getOutputStream=Illegal to call getOutputStream() after getWriter() has been called

http.method_not_implemented=Method {0} is not defined in RFC 2068 and is not supported by the Servlet API 

http.method_get_not_supported=HTTP method GET is not supported by this URL
http.method_post_not_supported=HTTP method POST is not supported by this URL
http.method_put_not_supported=HTTP method PUT is not supported by this URL
http.method_delete_not_supported=Http method DELETE is not supported by this URL

在这里插入图片描述
这是他的doget处理方法,通过获取http.method_get_not_supported获取valueHTTP method GET is not supported by this URL,所以当你servlet继承这个类的时候,doget用父类的处理方式,就会出现这个错误,就是从这里拿到的
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值