过滤 html标签(转)

来源:互联网 发布:菜鸟网络联系电话 编辑:程序博客网 时间:2024/06/05 18:52

    遇到个问题

 

   就是文本域内要是被人书写恶意的js 代码或者html的话

   会对整个系统产生严重的影响

   找了些资料

   自己也正在学习中...

  网上转载一篇相关文章

 代码:

  1. import java.util.regex.Pattern;
  2.   public class Test
  3.   {
  4.   
  5.   public static void main(String[] args)
  6.   {
  7.   String ww="<html>sss<body>ss</body>ssss</html>";
  8.   String ff=html2Text(ww);
  9.   System.out.println(ff);
  10.   }
  11.  public static String html2Text(String inputString) {
  12.         String htmlStr = inputString; // 含html标签的字符串
  13.         String textStr = "";
  14.         java.util.regex.Pattern p_script;
  15.         java.util.regex.Matcher m_script;
  16.         java.util.regex.Pattern p_style;
  17.         java.util.regex.Matcher m_style;
  18.         java.util.regex.Pattern p_html;
  19.         java.util.regex.Matcher m_html;
  20.         try {
  21.             String regEx_script = "<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?///[//s]*?script[//s]*?>"// 定义script的正则表达式{或<script>]*?>[/s/S]*?<//script>
  22.             // }
  23.             String regEx_style = "<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?///[//s]*?style[//s]*?>"// 定义style的正则表达式{或<style>]*?>[/s/S]*?<//style>
  24.             // }
  25.             String regEx_html = "<[^>]+>"// 定义HTML标签的正则表达式
  26.             p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
  27.             m_script = p_script.matcher(htmlStr);
  28.             htmlStr = m_script.replaceAll(""); // 过滤script标签
  29.             p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
  30.             m_style = p_style.matcher(htmlStr);
  31.             htmlStr = m_style.replaceAll(""); // 过滤style标签
  32.             p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
  33.             m_html = p_html.matcher(htmlStr);
  34.             htmlStr = m_html.replaceAll(""); // 过滤html标签
  35.             textStr = htmlStr;
  36.         } catch (Exception e) {
  37.             System.err.println("Html2Text: " + e.getMessage());
  38.         }
  39.         return textStr;
  40. }
  41.   }

 

struts中的html过滤

  1. public static String filter(String value)
  2.     {
  3.         if(value == null || value.length() == 0)
  4.             return value;
  5.         StringBuffer result = null;
  6.         String filtered = null;
  7.         for(int i = 0; i < value.length(); i++)
  8.         {
  9.             filtered = null;
  10.             switch(value.charAt(i))
  11.             {
  12.             case 60// '<'
  13.                 filtered = "<";
  14.                 break;
  15.             case 62// '>'
  16.                 filtered = ">";
  17.                 break;
  18.             case 38// '&'
  19.                 filtered = "&";
  20.                 break;
  21.             case 34// '"'
  22.                 filtered = """;
  23.                 break;
  24.             case 39// '/''
  25.                 filtered = "'";
  26.                 break;
  27.             }
  28.             if(result == null)
  29.             {
  30.                 if(filtered != null)
  31.                 {
  32.                     result = new StringBuffer(value.length() + 50);
  33.                     if(i > 0)
  34.                         result.append(value.substring(0, i));
  35.                     result.append(filtered);
  36.                 }
  37.             } else
  38.             if(filtered == null)
  39.                 result.append(value.charAt(i));
  40.             else
  41.                 result.append(filtered);
  42.         }
  43.         return result != null ? result.toString() : value;
  44.     }

 

 

  大家互相学习 哦

   

原创粉丝点击