2011年2月7日 星期一

將字串編碼成 html 格式

在寫 servlets/JSPs 時, 常要要輸出符合 html 格式的字串, 例如:

"bread" & "butter"  要變成 "bread" & "butter"


有現成的套件可以做此轉換.

org.apache.commons.lang.StringEscapeUtils 這個類別提供了:

public static String escapeHtml(String str)

可以將字串轉成 html 的格式.

Reference:
* Apache Commons
* escapeHtml()
* Robin's Tech Tips

沒有留言: