java屏蔽关键字_替换禁用语(指定关键字)的过滤器
功能描述
將請(qǐng)求響應(yīng)中所有的禁用關(guān)鍵字替換掉之后再輸出。
使用方法
在 java web 項(xiàng)目的 web.xml 文件中添加如下代碼。
StopWordsFilter
com.hmw.filter.StopWordsFilter
需要禁用的關(guān)鍵字,一個(gè)關(guān)鍵字占一行
keys
百度
七一五
StopWordsFilter
*.jsp
過濾器源碼
ReplaceKeyWordFilter.java
package?com.hmw.filter;
import?java.io.IOException;
import?java.io.PrintWriter;
import?java.util.HashSet;
import?java.util.Iterator;
import?java.util.Set;
import?java.util.StringTokenizer;
import?javax.servlet.Filter;
import?javax.servlet.FilterChain;
import?javax.servlet.FilterConfig;
import?javax.servlet.ServletException;
import?javax.servlet.ServletRequest;
import?javax.servlet.ServletResponse;
import?javax.servlet.http.HttpServletResponse;
/**
*?替換關(guān)鍵字的濾器
*
*?@author
*/
public?class?StopWordsFilter?implements?Filter?{
private?Set?keyWords?=?new?HashSet();
/**
*?將需要進(jìn)行替換的關(guān)鍵字添加到一個(gè)定義好的?Set?中
*/
@Override
public?void?init(FilterConfig?config)?throws?ServletException?{
String?keys?=??config.getInitParameter("keys");
StringTokenizer?tokenizer?=?new?StringTokenizer(keys);
String?token?=?null;
while?(tokenizer.hasMoreTokens())?{
token?=?tokenizer.nextToken();
if(token?!=?null?&&?token.length()?>?0){
keyWords.add(tokenizer.nextToken());
}
}
}
@Override
public?void?doFilter(ServletRequest?request,?ServletResponse?response,
FilterChain?chain)?throws?ServletException,?IOException?{
CharArrayWrapper?responseWrapper?=?new?CharArrayWrapper(
(HttpServletResponse)?response);
//?調(diào)用請(qǐng)求資源(使用自己包裝的?responseWrapper)
chain.doFilter(request,?responseWrapper);
//?取得響應(yīng)字符串
String?responseString?=?responseWrapper.toString();
//?將需要替換的關(guān)鍵字用“**”替換掉
Iterator?iter?=?keyWords.iterator();
while?(iter.hasNext())?{
responseString?=?replace(responseString,?iter.next(),?"**");
}
//?修改響應(yīng)頭信息中的?Content-Length
response.setContentLength(responseString.length());
PrintWriter?out?=?response.getWriter();
out.write(responseString);
}
@Override
public?void?destroy()?{
}
/**
*?將字符串中的所有的指定子字符串替換掉
*?@param?mainString?需要進(jìn)行替換的字符串
*?@param?orig?需要被替換的子串
*?@param?replacement?替換后的新串
*?@return?返回替換后的字符串
*/
public?static?String?replace(String?mainString,?String?orig,?String?replacement)?{
String?result?=?"";
int?oldIndex?=?0;
int?index?=?0;
int?origLength?=?orig.length();
while?((index?=?mainString.indexOf(orig,?oldIndex))?!=?-1)?{
result?=?result?+?mainString.substring(oldIndex,?index)?+?replacement;
oldIndex?=?index?+?origLength;
}
result?=?result?+?mainString.substring(oldIndex);
return?result;
}
}
CharArrayWrapper.java
package?com.hmw.filter;
import?java.io.CharArrayWriter;
import?java.io.PrintWriter;
import?javax.servlet.http.HttpServletResponse;
import?javax.servlet.http.HttpServletResponseWrapper;
/**
*?A?response?wrapper?that?takes?everything?the?client?would?normally?output?and
*?saves?it?in?one?big?character?array.
*/
public?class?CharArrayWrapper?extends?HttpServletResponseWrapper?{
private?CharArrayWriter?charWriter;
/**
*?Initializes?wrapper.
*?
*?First,?this?constructor?calls?the?parent?constructor.?That?call?is
*?crucial?so?that?the?response?is?stored?and?thus?setHeader,?*setStatus,
*?addCookie,?and?so?forth?work?normally.
*?
*?Second,?this?constructor?creates?a?CharArrayWriter?that?will?be?used?to
*?accumulate?the?response.
*/
public?CharArrayWrapper(HttpServletResponse?response)?{
super(response);
charWriter?=?new?CharArrayWriter();
}
/**
*?When?servlets?or?JSP?pages?ask?for?the?Writer,?don't?give?them?the?real
*?one.?Instead,?give?them?a?version?that?writes?into?the?character?array.
*?The?filter?needs?to?send?the?contents?of?the?array?to?the?client?(perhaps
*?after?modifying?it).
*/
@Override
public?PrintWriter?getWriter()?{
return?new?PrintWriter(charWriter);
}
/**
*?Get?a?String?representation?of?the?entire?buffer.
*?
*?Be?sure?not?to?call?this?method?multiple?times?on?the?same
*?wrapper.?The?API?for?CharArrayWriter?does?not?guarantee?that?it
*?"remembers"?the?previous?value,?so?the?call?is?likely?to?make?a?new
*?String?every?time.
*/
@Override
public?String?toString()?{
return?charWriter.toString();
}
/**?Get?the?underlying?character?array.?*/
public?char[]?toCharArray()?{
return?charWriter.toCharArray();
}
}
總結(jié)
以上是生活随笔為你收集整理的java屏蔽关键字_替换禁用语(指定关键字)的过滤器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java继承对象转换_java中类与对象
- 下一篇: layui表格更改一列数据_layui数