1.防止html注入
背景:提交订单时测试数据通常会有html标签,例如名称字段:<html>名称</html>,这样对于查询再显示出来会有问题,因此需要对html代码进行转义
(1)后台转义
-
String s = HtmlUtils.htmlEscape(" <div>hello world </div> <p> </p>"); //转义
-
System.out.println(s);
-
String s2 = HtmlUtils.htmlUnescape(s); //反转义
-
System.out.println(s2);
-
<div>hello world </div> <p> </p>
-
-
<div>hello world </div> <p> </p>
(2)前台转义
-
var HTMLEncode=function(input){
-
var converter=document.createElement('DIV');
-
converter.innerText=input;
-
var output=converter.innerHTML;
-
converter=null;
-
return output;
-
}
调用以上function
HTMLEncode("字段值")