Some words are not allowed to be published when we chat or leave messages. We can use filters to implement this function.
We just simply use filters to implement this filter function, and some places are not written in full
Front Desk Code:
<body> <form action="<c:url value='/WordServlet'//>" method="post"> Name: <input type="text" name="name"/><br/> Message content: <textarea rows="10" cols="10" name="textarea"></textarea><br/> <input type="submit" value="submit"/> </form> </body>
Code in Servlet:
Just read out the data collected from the front desk. Look at the sensitive words in it, all filter.
Code:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name =request.getParameter("name");//Name String text=request.getParameter("textarea");//Browse content PrintWriter pw =response.getWriter(); pw.println("name="+name);//Output directly here just to see if those keywords can be filtered. . pw.print("content"+text); }Filter:
The function of filtering can be reflected, and the filter intercepts between clients accessing the server.
We know that filters can control request and response, so we can start with this.
Request from the client is a request, so we only need to intercept it halfway and modify the value inside to achieve filtering. Adopt the packaging design model;
Filter code:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req=(HttpServletRequest) request; MyFilter myf =new MyFilter(req); chain.doFilter(myf, response);//Caiyong request of the class we strengthened, pass it to the subsequent servlet for use}Write a myFilter class manually to modify the functions we need to use.
class MyFilter extends HttpServletRequestWrapper{ //This is using the wrapper mode public MyFilter(HttpServletRequest request) { super(request); } @Override //Write this method public String getParameter(String name) { String words =super.getParameter(name); System.out.println(words); //Filter text List<String> list=WordUtils.getword(); for(String ll:list){ words=words.replace(ll, "*");//Sensitive words are replaced by **} return words; }In order to facilitate maintenance, we have specially written a tool for obtaining sensitive vocabulary, which is convenient for the administrator to add.
public class WordUtils { //Use singleton mode private static List<String> list =new ArrayList<String>();//We can access sensitive words stored in the database from this and encapsulate them into list to return static {//Manually add several list.add("scam"); list.add("swear"); list.add("silly"); } public static List<String> getword(){ return list; } public static void addWord(String name){ list.add(name); } public static void sava(){ // Here you can store the data in the list in the database for easy maintenance, and of course you can also write additions, deletions, modifications, and searches}Here, as long as the word contains, everything will become *
renderings;
Summary: The power of the filter is that you can modify the request and response objects together. What function you need to call, we can modify the function in the form of a packaging design pattern to become the effect we want. This is a bit similar to the proxy design pattern.
The above is the relevant knowledge of the method of using JavaEE filter to filter sensitive words introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!