JavaEE --------- filter)
Some words are not allowed to be published when we chat or leave a message. We can use filters to implement this function.
We simply use the filter to implement this filtering function, and some of them are not fully written.
Front-end code:
The code in Servlet:
Only read the data received from the front-end. All sensitive words are filtered.
Code:
Public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String name = request. getParameter ("name"); // name String text = request. getParameter ("textarea"); // browsed content PrintWriter pw = response. getWriter (); pw. println ("name =" + name); // output directly here, just to check whether the keywords can be filtered .. Pw. print ("content" + text );}
Filter:
The function of filtering can be reflected. The filter is intercepted between the client access servers.
We know that the filter can control the request and response, so we can start with this. The request from the client is a request, so we only need to intercept it halfway and modify the value to implement filtering. The packaging design mode 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 of the enhanced class is used by the servlet}
Manually write a myFilter class to modify the functions we need.
Class MyFilter extends HttpServletRequestWrapper {// This is the public MyFilter (HttpServletRequest request) {super (request) ;}@ Override // write this method from public String getParameter (String name) {String words = super. getParameter (name); System. out. println (words); // The text List before Filtering
List = WordUtils. getword (); for (String ll: list) {words = words. replace (ll, "*"); // replace sensitive words with **.} return words ;}
In order to facilitate maintenance, a special tool is provided for the acquisition of sensitive words, which is convenient for the Administrator to add.
Public class WordUtils {// single-instance mode private static List
List = new ArrayList
(); // We can encapsulate the sensitive words stored in the ACCESS database as a list and return static {// manually add several lists. add ("goods"); list. add (""); list. add ("silly");} public static List
Getword () {return list;} public static void addWord (String name) {list. add (name);} public static void sava () {// you can store the data in the list in the database for easy maintenance. Of course, you can also add, delete, modify, and query data}
Here, all the words that contain this word will become *
;
Summary: The filter is powerful. You can modify the request and response objects together. If you need to call any function, we can modify the function in the packaging design mode to achieve the desired effect, this is a bit similar to the proxy design pattern.