先去apache下一个commons-lang包
然后建一个hasmap,把特殊字符与他的实体建立关系。
写一个利用lang中的replacesubsitute把实体替换特殊字符就行了。

解决方案 »

  1.   

    天呀,这么复杂,没有php,asp那样简单的一个函数么?
      

  2.   

    strText=strText.replaceAll("<","&lt;");
    strText=strText.replaceAll(">","&gt;");
    strText=strText.replaceAll("\"","&quot;");
    …………
    或者用iterate标签,自动转换。
      

  3.   

    iterate标签是什么东西?小弟不懂
      

  4.   

    晕,错了,是bean:write 标签
      

  5.   

    本身在jsp中的语法就有对这些特殊符号的替代呀!
    ("<","&lt;");(">","&gt;");("\"","&quot;");等
      

  6.   

    呵呵,高手! 那用replaceAll替换要替换多少字符,除了“<>/”,还有什么需要替换的?
      

  7.   

    呵呵,这个就多了,你可以打开dreamweaver 对照着看看,通常比较主要的就是<>“”回车 空格
      

  8.   

    参考:
    http://expert.csdn.net/Expert/topic/3061/3061835.xml?temp=.9889185
    这里有一个,不过要用到apache的lang包
    下载地址:
    http://www.apache.org/dist/jakarta/commons/lang/import org.apache.commons.lang.StringUtils;
    import java.util.*;
    public class HTMLHelper {
        /**
         忽略了ISO Latin-1 特殊字符以及
         &ensp;半个空白位
         和&emsp;   一个空白位     加入了"\"     &lt; < 小于号或显示标记
         &gt; > 大于号或显示标记
         &amp; & 可用于显示其它特殊字符
         &quot; " 引号
         &reg; \u00AE 已注册
         &copy; \u00A9 版权
         &trade; \u2122 商标
         &nbsp;   不断行的空白
         */
        public final static HashMap specialCharSet = new HashMap();
        static {
            specialCharSet.put("<","&lt");
            specialCharSet.put(">","&gt;");
            specialCharSet.put("&","&amp;");
            specialCharSet.put("\"","&quot;");
            specialCharSet.put("\u00AE","&reg;");
            specialCharSet.put("\u00A9","&copy;");
            specialCharSet.put("\u2122","&trade;");
            specialCharSet.put(" ","&nbsp;");
        }    /**
         * 替换所有的HTML字符(主要为javascript所用)
         * @param s
         * @return
         */
        public static String substitute(String s){
            s=s.trim();
            for(Iterator i = specialCharSet.keySet().iterator();i.hasNext();){
                String repl=(String)i.next();
                String with=(String)specialCharSet.get(repl);
                s=StringUtils.replace(s,repl,with);
            }
            return s;
        }
        public static void main(String[] args) {
            System.out.println(substitute("\\tzzf\\\u00AE\u00A9\u2122\"&<>ddd  "));
        }
    }
      

  9.   

    真的太感谢各位大哥了,还是决定用字符替换的简单方法,只是遗憾java毕竟没有php,asp那么处理方便
      

  10.   

    可惜只有20分奉送,见谅,本人在上海,有空msn聊,我的号码是:[email protected]