2013-02-23 15 views
0

如何在jsp的.properties文件中保存參數值,如鍵值對。如何保存.property文件中的參數值?

例如,
url: http://www.xyz.com/login.jspFNAME =哈利& L-NAME =陶工&職業=演員

.property文件必須看起來像這樣
FNAME =哈利
L-NAME =陶工
職業=演員

是有可能?

在此先感謝

+0

你是什麼意思'有可能'?你的建議有什麼問題? – 2013-02-23 10:35:46

+0

@ bmorris591我google了但沒有找到我想要的 – lata 2013-02-23 10:37:29

+0

@ bmorris591還沒有嘗試過,但想知道1st可能嗎? – lata 2013-02-23 10:41:41

回答

2

這個怎麼樣:

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,把PARAMS成Properties對象時,它會然後將其寫入文件。

請注意,如果您的URL字符串中有任何重複的鍵,它們將被覆蓋,因此此方法無效。在這種情況下,您可能需要查看Apache HttpComponents

+0

gr8!它運作良好! :) 非常感謝 :) – lata 2013-02-23 11:15:36

1

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

+0

謝謝回覆atleast :) – lata 2013-02-23 11:07:12

相關問題