Java 如何在.property文件中保存参数值?

Java 如何在.property文件中保存参数值?,java,jquery,jsp,url,servlets,Java,Jquery,Jsp,Url,Servlets,如何在jsp的.properties文件中保存参数值及其值(如键值对) 例如 网址: ?fname=harry&lname=potter&accounty=actor .property文件必须如下所示 fname=harry lname=potter 职业=演员 可能吗 提前感谢检查java.util.Properties.store(OutputStream,String)和java.util.Properties.store(Writer,String)(java 1.6)检查java

如何在jsp的.properties文件中保存参数值及其值(如键值对)

例如
网址: ?fname=harry&lname=potter&accounty=actor

.property文件必须如下所示
fname=harry
lname=potter
职业=演员

可能吗


提前感谢

检查
java.util.Properties.store(OutputStream,String)
java.util.Properties.store(Writer,String)
(java 1.6)

检查
java.util.Properties.store(OutputStream,String)
java.util.Properties.store(Writer,String)
(java 1.6)

<

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字符串中有任何重复的键,它们将被覆盖,因此此方法将不起作用。在这种情况下,您可能需要看一看。

这个怎么样:

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字符串中有任何重复的键,它们将被覆盖,因此此方法将不起作用。在这种情况下,您可能想看看。

您所说的“可能”是什么意思?你的建议怎么了?@bmorris591我在谷歌上搜索了一下,但没有找到我想要的want@bmorris591还没试过,但想知道有可能吗?当然有可能。为什么不呢?投票结束。但这不是JSP的工作。servlet应该做到这一点。你所说的“可能”是什么意思?你的建议怎么了?@bmorris591我在谷歌上搜索了一下,但没有找到我想要的want@bmorris591还没试过,但想知道有可能吗?当然有可能。为什么不呢?投票结束。但这不是JSP的工作。servlet应该做到这一点。