我在jsp的代码里使用了ewebedit编辑器精简版(纯javascript)在添加页正常使用但是在修改页面就常常出错asp里有server.htmlencode这样的方法来阻止代码在编辑器里执行,jsp里没有这样的函数我试了去处和转换数据库字段的东西
添加了htmlencode函数但还是不行
这种问题在jsp里怎么解决?

解决方案 »

  1.   

    好像我以前用过一个rte就是自己写了一个encode函数把特殊标记驱除的
      

  2.   

    public final class HtmlFunction{
         /**
          *编码html字符
          */
         public static String htmlEncode(String str){
         
           String txt = str;
         
             txt = txt.replaceAll("&","&");
             
             txt = txt.replaceAll("&","&");
             
             txt = txt.replaceAll(""",""");
             
             txt = txt.replaceAll("/",""");
             
             txt = txt.replaceAll("<","<");
             
             txt = txt.replaceAll("<","&lt;");
             
             txt = txt.replaceAll("&amp;gt;","&gt;");
             
             txt = txt.replaceAll(">","&gt;");
             
             txt = txt.replaceAll("&amp;nbsp;","&nbsp;");
             
             txt = txt.replaceAll(" ","&nbsp;");
             
          return txt;
         }
         
         /**
          *反编码html字符
          */
         public static String unHtmlEncode(String str){
         
          String txt = str;
         
             txt = txt.replaceAll("&amp;","&");
             
             txt = txt.replaceAll("&quot;","/");
             
             txt = txt.replaceAll("&lt;","<");
             
             txt = txt.replaceAll("&gt;",">");
             
             txt = txt.replaceAll("&nbsp;"," ");
             
          return txt;
         }
         
         /**
          *去除str的HTML格式
          */
         public static String clearHtml(String str){
         
          //将regex编译到模式,不区分大小写
          Pattern p = Pattern.compile("<[^>]+>|</[^>]+>",Pattern.CASE_INSENSITIVE );
         
        Matcher m = p.matcher(str);
        
      return m.replaceAll("");
         
         }
    }
      

  3.   

    楼上的swoky兄弟
    我是asp程序员,现在还不太会jsp,那个用类的代码要怎么用啊?
      

  4.   

    无语运行一下这个HTML<script>
    //write by theForever csdn.net
    function ClearHtmlCode(txt){
    txt=txt.replace(/&/g,   "&amp;");
    txt=txt.replace(/</g,   "&lt;");
    txt=txt.replace(/>/g,   "&gt;");
    return txt;
    }
    </script>
    <input type='button' onclick="txt.value=ClearHtmlCode(txt.value);" value='替换后面文本HTML标记'>
    <input id=txt type='text' value='<html>去掉&lt;>sksj<nikb >'>
      

  5.   

    在webedit的官方网站找到如下代码,问题解决!
    http://ewebeditor.webasp.net/manual/#_Toc160644280
    <%!static String htmlEncode(int i){     if (i=='&') return "&amp;";     else if (i=='<') return "&lt;";     else if (i=='>') return "&gt;";     else if (i=='"') return "&quot;";     else return ""+(char)i;}     static String htmlEncode(String st){     StringBuffer buf = new StringBuffer();     for (int i = 0;i<st.length();i++){         buf.append(htmlEncode(st.charAt(i)));     }     return buf.toString();}%>
    <textarea name="content1" style="display:none"><%=htmlEncode(str)%></textarea><IFRAME ID="eWebEditor1" src="../ewebeditor.htm?id=content1&style=coolblue" frameborder="0" scrolling="no" width="550" height="350"></IFRAME>
      

  6.   

    碧海晴天的那个要是onload时执行可能可以吧!,谢谢!