如何在jsp的.properties文件中保存参数值及其值,如键值对
例如。
网址:
http ://www.xyz.com/login.jsp ?fname=harry&lname=potter&occupation=actor
.property 文件必须如下所示
fname=harry
lname=potter
职业=actor
是否可以?
提前致谢
那这个呢:
final String urlString = "http://www.xyz.com/login.jsp?fname=harry&lname=potter&occupation=actor";
final URL url;
try {
url = new URL(urlString);
} catch (MalformedURLException ex) {
throw new RuntimeException(ex);
}
final Properties properties = new Properties();
for (final String param : url.getQuery().split("\\&")) {
final String[] splitParam = param.split("=");
properties.setProperty(splitParam[0], splitParam[1]);
}
for (final String key : properties.stringPropertyNames()) {
System.out.println("Key " + key + " has value " + properties.getProperty(key) + ".");
}
final FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream(new File("My File"));
} catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
}
try {
properties.store(fileOutputStream, "Properties from URL '" +urlString + "'.");
} catch(IOException ex) {
throw new RuntimeException(ex);
} finally {
try {
fileOutputStream.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
这将解析 URL 并将参数放入Properties
对象中,然后将其写入文件。
请注意,如果您的 URL 字符串中有任何重复的键,它们将被覆盖,因此此方法将不起作用。在这种情况下,您可能想看看Apache HttpComponents。
检查java.util.Properties.store(OutputStream, String)
和java.util.Properties.store(Writer, String)
(Java 1.6)