代碼如下:
/**
* html代碼輸入驗(yàn)證轉(zhuǎn)換
* @param str
* @return
*/
public String htmlFilter(String str){
//轉(zhuǎn)意&
str = str.replaceAll("&", "&");
//轉(zhuǎn)意<
str = str.replaceAll("<", "<");
//轉(zhuǎn)意>
str = str.replaceAll(">", ">");
//轉(zhuǎn)意空格,
//注意:空格使用" "會(huì)導(dǎo)致xml解析出錯(cuò)!,這里只能使用" "
str = str.replaceAll(" ", " ");
//轉(zhuǎn)意單引號(hào)
str = str.replaceAll("'", "'");
//轉(zhuǎn)意雙引號(hào)
str = str.replaceAll("\"", """);
//轉(zhuǎn)意換行
str = str.replaceAll("\n", "<br>");
return str;
}
//本方法用于在servlet生成xml代碼中再次轉(zhuǎn)意,主要是把<br>轉(zhuǎn)換為正常顯示
public String htmlFilter2(String str){
//轉(zhuǎn)意<
str = str.replaceAll("<", "<");
//轉(zhuǎn)意>
str = str.replaceAll(">", ">");
return str;
}
更多信息請(qǐng)查看IT技術(shù)專欄