50,528
社区成员
发帖
与我相关
我的任务
分享
public class PropertiesUtils {
static Properties prop = new Properties();
/**
* @param fileName 需要加载的properties文件,文件需要放在src根目录下
* 是否加载成功
*/
public static boolean loadFile(String fileName){
try {
prop.load(PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName));
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 根据KEY取回相应的value
*/
public static String getPropertyValue(String key){
return prop.getProperty(key);
}
}
二、创建properties文件
##jdbc config
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8
username = root
password = 123456
三、servlet获取properties文件参数
PropertiesUtils.loadFile("config.properties");
String username = PropertiesUtils.getPropertyValue("username");
String password = PropertiesUtils.getPropertyValue("password");
request.setAttribute("username", username);
request.setAttribute("password", password);
四、jsp获取
<input type="text" name="<%=request.getAttribute("username")%>" value="<%=request.getAttribute("password")%>" />
使input输入框的name属性为root,value值为123456