j2ee中jsp获取properties文件中的值的方法

1. 写一个java 类, 在网站启动的时候,加载 这个java类, 在这个java类里面设置一个静态变量,并做操作加载资源文件 

abc.properties 并将获取到的值保存入 这个静态变量。 这样你可以再任何一个 jsp 页面直接引用这个java类就可以得到配置文件里面的值,这样设计可以达到一个代码复用的效果。不需要在其他页面重新进行加载文件。

2.这个是java类

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Constant {
    

    public static String oneLevel = "";
    public static String twoLevel = "";
    public static String threeLevel = "";
    public static String fourLevel = "";

        static {
            try {
                InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("level.properties");
                Properties p = new Properties();
                p.load(is);
                oneLevel = p.getProperty("common.one");
                twoLevel = p.getProperty("common.two");
                threeLevel = p.getProperty("common.three");
                fourLevel = p.getProperty("common.four");
                
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

3.这个是jsp页面

<%@page import="com.newtouch.las.Constants"%>

System.out.println("========="+Constant.oneLevel+"=="+Constant.twoLevel+""+Constant.threeLevel+""+Constant.fourLevel);
4.这个是porpertise文件
<pre name="code" class="html">common.one =10
common.two = 100
common.three = 1000
common.four = 10000


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值