Jsp获取Properties配置文件、java类获取配置文件

4 篇文章 0 订阅
2 篇文章 0 订阅

1、JSP获取配置文件

<%@ page contentType="text/html; charset=UTF-8" import="java.util.ResourceBundle" %>
<script type="text/javascript">
     <%ResourceBundle res = ResourceBundle.getBundle("setTime"); %>  <!--这里填写properties文件的名称-->
     <%String socketUrl = res.getString("simpleTimer.timer"); %>
     var socketUrl = "<%=socketUrl %>";
     console.log(socketUrl); 
</script>

2、Jsp获取当前项目路径,

在jsp顶部添加以下代码

①、<% 
    String path1 = request.getContextPath(); 
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path1+"/"; 
%> 

②、<%
	pageContext.setAttribute("APP_PATH", request.getContextPath());
	String path = request.getScheme() + "://" + request.getServerName()
	        + ":" + request.getServerPort() + request.getContextPath()
	        + "/";
	pageContext.setAttribute("path", path);
%>

使用时:

①、var url2='<%=basePath%>';

②、<img class="pimg" src="${APP_PATH}/static/images/a14053fbf3501dd586cd466edf280a68.jpg" width="200px"  height="200px"/> 

3、Java类获取properties文件

public class GetProperty {  
    
    public GetProperty() {  
        super();  
    }  
  

    
    // 方法:通过类加载目录getClassLoader()加载属性文件    
    public static String getPropertyByName2(String path, String name) {    
        String result = "";    
    
        // 方法二:通过类加载目录getClassLoader()加载属性文件    
        InputStream in = GetProperty.class.getClassLoader()    
                .getResourceAsStream(path);    
        // InputStream in =    
        // this.getClass().getClassLoader().getResourceAsStream("mailServer.properties");    
    
        // 注:Object.class.getResourceAsStream在action中调用报错,在普通java工程中可用    
        // InputStream in =    
        // Object.class.getResourceAsStream("/mailServer.properties");    
        Properties prop = new Properties();    
        try {    
            prop.load(in);    
            result = prop.getProperty(name).trim();    
          //  System.out.println("name:" + result);    
        } catch (IOException e) {    
            System.out.println("读取配置文件出错");    
            e.printStackTrace();    
        }    
        return result;    
    }    
      
  /*  public static void main(String[] args) {  
       // System.out.println("=="+GetProperty.getPropertyByName("dbconfig.properties","jdbc.jdbcUrl"));    
        System.out.println("==>>"+GetProperty.getPropertyByName2("dbconfig.properties","jdbc.jdbcUrl"));    
    }  */
}  

String timer=GetProperty.getPropertyByName2("setTime.properties","simpleTimer.timer");


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值