http://blog.csdn.net/u012836851/article/details/52468231 How JAVA daemons and JSP pages get the values in the properties file

http://blog.csdn.net/u012836851/article/details/52468231



JAVA daemon and JSP page how to get the value in the properties file

original  2016-09-08 10:17:00

        In actual development, we often write some properties into the properties file to facilitate our operations in development or maintenance. The benefits of this are very obvious. When our needs or operating environment change, we only need to change the corresponding configuration files to meet the requirements. But doing this also brings some development problems, such as how do we get the values ​​in the properties file.


ApplicationProperties.properties configuration file content:

FtpUrl = http\://192.168.0.14\:8080/

一、
JSP文件中获取该属性值得方法:

<%@ page language="java" import="java.util.*,com.sdzn.entity.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ path + "/";
    // properties 配置文件名称
    ResourceBundle res = ResourceBundle.getBundle("ApplicationResources");
%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <base href="<%=basePath%>">
        <title>Test</title>
        <script type="text/javascript">
            // Get the property value in the properties configuration file
            var ftpurl= <%=res.getString ("FtpUrl ")%>;
        </script>
    </head>
    <body ></body>
</html>

Second, the method to get the property value in the Java background program:

import java.util.PropertyResourceBundle;

// properties configuration File name
PropertyResourceBundle res = (PropertyResourceBundle) PropertyResourceBundle.getBundle("ApplicationResources");
// Get the property value in the properties configuration file
String ftpurl = res.getString("FtpUrl ");


        在实际开发中,我们经常会将一些属性写入到properties文件中,方便我们在开发或维护中的操作。这样做的好处十分明显,当我们需求或运行环境发生变化时,只需更改相应的配置文件即可符合要求。但这样做的同时也带来了一些开发上的问题,比如我们如何获取properties文件中的值。


ApplicationProperties.properties配置文件内容:

FtpUrl = http\://192.168.0.14\:8080/

一、
JSP文件中获取该属性值得方法:

<%@ page language="java" import="java.util.*,com.sdzn.entity.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ path + "/";
    // properties 配置文件名称
    ResourceBundle res = ResourceBundle.getBundle("ApplicationResources");
%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <base href="<%=basePath%>">
        <title>测试</title>
        <script type="text/javascript">
            // 获取properties配置文件中的属性值
            var ftpurl= <%=res.getString("FtpUrl ")%>;
        </script>
    </head>
    <body ></body>
</html>

二、Java后台程序中获取该属性值得方法:

import java.util.PropertyResourceBundle;

// properties 配置文件名称
PropertyResourceBundle res = (PropertyResourceBundle) PropertyResourceBundle.getBundle("ApplicationResources");
// 获取properties配置文件中的属性值
String ftpurl = res.getString("FtpUrl ");


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325904222&siteId=291194637