永发信息网

如何设计jsp页面读取配置文件

答案:1  悬赏:0  手机版
解决时间 2021-03-31 22:07
  • 提问者网友:未信
  • 2021-03-31 11:53
如何设计jsp页面读取配置文件
最佳答案
  • 五星知识达人网友:夜风逐马
  • 2021-03-31 13:22
package com.ydq.read.propertyfile;
2.
3.import java.io.InputStream;
4.import java.util.MissingResourceException;
5.import java.util.Properties;
6.import java.util.ResourceBundle;
7.
8.
26.public class CommonParam {
27.
28. private String propertyFileName;
29. private ResourceBundle resourceBundle;
30.
31. public CommonParam() {
32. propertyFileName = "ydqfile";
33. resourceBundle = ResourceBundle.getBundle(propertyFileName);
34. }
35.
36. public String getString(String key) {
37. if (key == null || key.equals("") || key.equals("null")) {
38. return "";
39. }
40. String result = "";
41. try {
42. result = resourceBundle.getString(key);// 根据key获取value
43. } catch (MissingResourceException e) {
44. e.printStackTrace();
45. }
46. return result;
47. }
48.
49. public static void main(String[] args) {
50.
57.
58.
61. CommonParam test = new CommonParam();
62. System.out.println(test.getPara("ydqfile.properties"));
63.
64. }
65.
66.
71. public String getPara(String fileName) {
72. Properties prop = new Properties();
73. try {
74. InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName);
75. prop.load(is);
76. if (is != null)
77. is.close();
78. } catch (Exception e) {
79. System.out.println(e + " file " + fileName + " not found");
80. }
81. return prop.getProperty("ydqb");
82. }追问首先谢谢 你的回答,但是你说的不是我想要的答案,注意我说的是设计,当然你说的hashtable的子类Properties可以做,但是性能太低了.这个问题我解决了.写个拦截器就搞定了.
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯