如何将 HTML 代码转换成实体字符
2个回答
展开全部
写个过滤器,转换参数。或是在后台方法中单独对参数进行转换
String str = filter("<script>");
public String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("& lt;");
break;
case '>':
result.append("& gt;");
break;
case '&':
result.append("& amp;");
break;
case '"':
result.append("& quot;");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
String str = filter("<script>");
public String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("& lt;");
break;
case '>':
result.append("& gt;");
break;
case '&':
result.append("& amp;");
break;
case '"':
result.append("& quot;");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询