用正则表达式可以过滤
你的还原是什么意思?非法字符还要还原?用replaceAll替换成转义字符吧

解决方案 »

  1.   

    把字符串用base64编码转换后再存入数据库,取出来的时候转换回来即可解决你的这个问题。
      

  2.   

    不好意思我没用说清楚, 我的意思是:
       如果将带有 HTML 标记的代码存到数据库中后, 如何将其还原?例如: 将<textarea></textarea>中的内容,存到数据库中,在再次读出的时候能够      按照原先的格式或形式输出到页面上;
    我刚写了两个函数:一个是过虑 Sql 语句的;一个是过虑 HTMLTags 的;请大家提提建议!!<%!
    /*================================================================*/
    /* 功能描述:                                                     */
    /*     替换 Sql 语句中的非法字符;                                */
    /* 返回值:                                                       */
    /*     重编码后的 Sql 语句或空串;                                */
    /* 参数说明:                                                     */
    /*     strSql:要进行重新编码的 Sql 语句;                        */
    /*================================================================*/
    String encodeSql(String strSql){
       if(strSql==null || strSql.length()==0)
       { return ""; }   int len,i;
       char c=' ';
       strSql=strSql.trim();
       len=strSql.length();
       StringBuffer buf=new StringBuffer(strSql.length()+6);
       for(i=0;i<len;i++){
          c=strSql.charAt(i);
          if(c=='\'') buf.append( "''");
          else if(c=='%') buf.append( "%%");
          else buf.append(c);
      }
      return buf.toString();
    }/*================================================================*/
    /* 功能描述:                                                     */
    /*     替换用户输入字符串中的非法字符;                           */
    /* 返回值:                                                       */
    /*     重编码后的字符串或空串;                                   */
    /* 参数说明:                                                     */
    /*     input:要进行重新编码的字符串;                            */
    /*================================================================*/
    String escapeHtmlTags(String input){
       if(input==null || input.length()==0)
       { return ""; }   int len,i;
       char c=' ';
       input=input.trim();
       len=input.length();
       StringBuffer buf=new StringBuffer(input.length()+6);
       for(i=0;i<len;i++){
          c=input.charAt(i);
          if(c=='<')      buf.append("&lt;");
          else if(c=='>') buf.append( "&gt;");
          else if(c=='&') buf.append( "&amp;");
          else if(c=='\'') buf.append( "''");
          else if(c=='%') buf.append( "%%");
          else if(c==' ') buf.append( "&nbsp;");
          else if(c=='\n') buf.append( "<br>");
          else if(c=='\r') buf.append( "<br>");
          else buf.append(c);
      }
      return buf.toString();
    }
    %>