A detailed example of the filtering method of Java EE filter sensitive words _java

Source: Internet
Author: User

When we chat or leave a message, some of the words are not allowed to be published. We can use filters to implement this function.

We simply use the filter to achieve this filtering function, some places are not written in full

Foreground code:

<body> 
<form action= "<c:url value= '/wordservlet '/>" method= "post" > 
name: <input type= " Text "name=" name "/><br/> 
message content: <textarea rows=" "cols=" ten "name=" textarea "></textarea> <br/> 
<input type= "Submit" value= "submitted"/> 
</form> 

The code inside the servlet:

Just read the data from the foreground. Look inside the sensitive words are all filtered.

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);//Direct output here, just to see if you can filter those keywords. 
pw.print ("content" +text); 
}

Filter:

The role of filtering can be reflected in the filter is in the client access to the server between the interception.

We know that the filters can control the request and the response, so we can do this.

Request from the client is requested, so we only need to intercept it halfway, modify the value of the filter can be implemented. The design mode of packaging is adopted;

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); The request for our enhanced class is passed to the following servlet 

Manually write a Myfilter class to modify the functions we need to use.

Class Myfilter extends httpservletrequestwrapper{ 
//This is in the packaging mode public 
myfilter (HttpServletRequest request) { 
super (request); 
} 
@Override//From writing this method public 
string GetParameter (string name) { 
string words =super.getparameter (name); 
SYSTEM.OUT.PRINTLN (words);//filter before the text 
list<string> List=wordutils.getword (); 
for (String ll:list) { 
words=words.replace (ll, "*");//Sensitive words use * * instead of 
} return 
words; 

In order to facilitate maintenance, our sensitive vocabulary acquisition specializes in writing a tool to facilitate access, of course, also facilitate the administrator to add.

public class Wordutils { 
//Use single case mode 
private static list<string> List =new arraylist<string> (); We can access the sensitive words stored in the database, encapsulate the list back to 
static {///Manually add a few 
list.add ("Pit goods"); 
List.add ("curse"); 
List.add ("silly"); 
} 
public static list<string> Getword () {return 
List; 
} 
public static void Addword (String name) { 
list.add (name); 
} 
public static void Sava () { 
//here can be stored in the list of data to the database, convenient maintenance, of course, can also write and delete changes to check, etc. 

Here, as long as the word is contained, all will become *

Effect chart;


Summary: The filter is powerful, you can modify the request and response objects together, you need to tune what function, we can use the packaging design mode to modify this function, become the effect we want, this and the agent design pattern is somewhat similar.

The above is a small set to introduce the use of Java EE Filter filter sensitive words of the relevant knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.