jsp页面简单用*号替换敏感词

1.在项目的web.xml写入

<context-param>
<param-name>noWords</param-name>
<param-value>你需要的敏感词</param-value>
</context-param>

2.再post提交页面写入

request.setCharacterEncoding("utf-8");
String name = request.getParameter("texts");  //获取到你写入的内容
String param = this.getServletContext().getInitParameter("noWords"); //查找到web.xml的敏感词
String[] text = param.split(",");  //分割用英文逗号隔开的铭感词存入数组
for(int i=0;i<text.length;i++){  //遍历数组
name = name.replace(text[i], "*");  //用replace方法把敏感词替换为*号
}
out.print(name);  //输出看看结果

 

posted on 2018-01-15 18:29  小欣柑  阅读(294)  评论(0编辑  收藏  举报

导航