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");