Filter some sensitive characters in the webpage using Java program

Source: Internet
Author: User

Import java. io. charArrayWriter; import java. io. printWriter; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpServletResponseWrapper; public class Ct extends HttpServletResponseWrapper {private CharArrayWriter output; // rewrite tostring method public String toString () {return output. toString ();} public Ct (HttpServletResponse response) {super (response); this. output = new CharArrayWriter ();} public PrintWriter getWriter () {return new PrintWriter (output) ;}// Filter import java. io. IOException; import java. io. printWriter; 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. httpServlet; import javax. servlet. http. httpServletResponse; public class HtmlFilter extends HttpServlet implements Filter {public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {// set the response code response. setCharacterEncoding ("UTF-8"); PrintWriter out = response. getWriter (); Ct ct Ct = new Ct (HttpServletResponse) response); // execute the next chain. doFilter (request, ct); String resStr = ct. toString (). trim (); String newStr = ""; if (resStr. contains (" Er") {newStr = resStr. replaceAll (" Er", "***");} out. print (newStr);} public void init (FilterConfig filterConfig) throws ServletException {// TODO Auto-generated method stub} // xml configuration
 
   
      
   
    
Index. jsp
     
      
      
    
   
    
Ecodeing
     
   
    
HtmlFilter
     
    
    
   
    
Ecodeing
     
   
    
*. Jsp
     
    
 // Jsp page <% @ page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %>              My JSP 'index. jsp 'starting page
 
 
     
 
 
       Your family?

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.