Sample Code for filtering Jsp sensitive words and sample code for jsp Filtering
Most forums and websites have set sensitive words for ease of management.
On most websites, sensitive words generally refer to words with sensitive political tendencies (or anti-ruling party tendencies), violent tendencies, unhealthy colors, or uncivilized languages, some websites also set some special sensitive words that only apply to the website based on their actual conditions.
For example, when you post a post with some predefined words, this post cannot be issued. Or the word is automatically replaced with an asterisk (*) or a cross sign (X), or is replaced with a harmonious word.
In my opinion, the most important thing for sensitive word filtering is to write a vocabulary filtering algorithm. How can I filter out a large number of sensitive words? I feel that DFA has a good idea.
DFA Overview
DFA is the only good algorithm to implement text filtering. DFA is the Deterministic Finite automatic, that is, the Deterministic Finite automatic machine. It obtains the next state through the event and the current state, that is, event + state = nextstate. Shows the transition of its status
In this figure, uppercase letters (S, U, V, and Q) are all states, and lowercase letters a and B are actions. We can see the following link:
A B
S -----> u s -----> v u -----> V
In the algorithm that implements sensitive word filtering, we must reduce the number of operations, while DFA has almost no calculation in the DFA algorithm, and some are only state conversion.
Implement the DFA Algorithm in Java to filter sensitive words
The key to sensitive word filtering in Java is the implementation of the DFA algorithm. First, we will analyze it. In this process, we think the following structure will be clearer.
At the same time, there is no state conversion, no action, and some are queries ). We can think that, through S query U, V, through U query V, P, through V query u p. Through this transformation, we can convert the state to the search using the Java Collection.
It is true that the following sensitive words are added to our sensitive dictionary: Japanese, Japanese devil, Mao ze.dong. So what kind of structure do I need to build?
First, query day ---> {book}, query book ---> {person, Ghost Child}, query person ---> {null}, query ghost ---> {Child }. Structure:
Below we are extending this figure:
In this way, we construct our sensitive dictionary into a tree similar to one another. In this way, we can determine whether a word is a sensitive word, which greatly reduces the Matching Scope of the search. For example, if we want to judge the Japanese, we can identify the tree to be retrieved based on the first word, and then search it in the tree.
This idea will be used later. I will first write a simple method for filtering words without involving algorithms.
Java code implementation
Basic Idea: rewrite the getParameter method in HttpServletRequestWrapper, let the user input the word through this filter, write a class to inherit it, rewrite the method, write a dictionary to filter the word, and compare it with the input
First, write a jsp page. js is refreshed using Ajax. Recently I want to try it. It feels good. Ajax needs to introduce js files.
<Body> <input type = "text" name = "word" onblur = "filter (this. value ); "id =" filter "/> <input type =" submit "value =" sensitive word filtering "/> <script type =" text/javascript "src =" js/jquery. js "> </script> <script type =" text/javascript "> function filter (num) {$. ajax ({type: "post", // submission url: "FilterWordServlet", async: true, // asynchronous request dataType: "html ", // data return type data: {"num": num}, // after the data is passed to the previous value success: function (data, textStatus) {// after successful execution, call back this function to process transactions $ ("# filter "). val (data) ;}, error: function () {// failed to execute this function and process the failed Transaction alert ("error ");}})} </script> </body>
Inherit HttpServletRequestWrapper and override the getParameter method,
// The main idea is to inherit HttpServletRequestWrapper and rewrite its getParameter method so that it can filter the public class WordFilter extends HttpServletRequestWrapper {public WordFilter (HttpServletRequest request) {super (request ); // TODO Auto-generated constructor stub} @ Override public String getParameter (String name) {// first obtain the parent's method and input the value, the obtained value is compared with that in the filter dictionary to check whether it contains the value. If yes, It is replaced. If no value is returned, String word = super is returned. getParameter (name); // call the text List in the dictionary <String> list = Words. getList (); for (String string: list) {// determines whether the dictionary contains the text if (word. contains (string) {// replace word = word in the string. replace (string, "**") ;}} return word ;}}
Write another Servlet to get the words entered by the user and filter the words.
@ WebServlet ("/FilterWordServlet") public class FilterWordServlet extends HttpServlet {private static final long serialVersionUID = 1L; protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// set request and encoding format request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); // The self-created Request Method inherits from the original one. The getParameter method is rewritten to enable it to filter the business WordFilter w. Filter = new WordFilter (request); String string = wFilter. getParameter ("num"); System. out. println ("---------------"); // response method of out. Output on the page, let ajax get this to process some business PrintWriter out = response. getWriter (); out. println (string);} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub doGet (request, response );}}
I have created a filter vocabulary for the list set. I can modify the search algorithm for the vocabulary from here, and create a vocabulary class first.
Public class Words {// dictionary for vocabulary filtering static List <String> list = new ArrayList <> (); static {list. add ("your sister's"); list. add ("sb"); list. add ("roll");} public static List <String> getList () {return list;} public static void setList (List <String> list) {Words. list = list ;}}
This is just the most basic one. I think this DFA algorithm is good for advanced implementation of algorithms.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.