往数据库里存的时候,把'\n'替换成chr(数值)形式,显示的时候再还原试试。

解决方案 »

  1.   

    把">"号替换成">"
    把"<"号替换成"&lt;"
    把"&"号替换成"&amp"
    空格替换成"&nbsp;"
    把回车符"\n"替换成"<br>"
    把双引号替换成"&quot;"
      

  2.   

    把'\n'替换成chr(数值)形式
    就是用xunyiren(从jsp开始)
    说的方法
      

  3.   

    package common;
    import java.io.*;public class CodeFilter{
      public CodeFilter() {}
      public static String change(String s) {
      s = toHtml(s);
      return s;
      } //特殊字符转为Html
    public static String toHtml(String s) {
        s = Replace(s,"&","&amp;");
        s = Replace(s,"<","&lt;");
        s = Replace(s,">","&gt;");
        s = Replace(s,"\t","    ");
        s = Replace(s,"\r\n","\n");
        s = Replace(s,"\n","<br/>");
        s = Replace(s,"  "," &nbsp;");
        s = Replace(s,"'","&#39;");
        s = Replace(s,"\\","&#92;");
        return s;
        }
    //逆
        public static String unHtml(String s){
    s = Replace(s,"<br/>","\n");
    s = Replace(s,"&nbsp;"," ");
    return s;
       }
     
      //Replace
       public static String Replace(String source,String oldString,String newString) {
        if(source == null) return null;
        StringBuffer output = new StringBuffer();
        int lengOfsource = source.length();
        int lengOfold = oldString.length();
        int posStart = 0;
        int pos;
        while((pos = source.indexOf(oldString,posStart)) >= 0) {
          output.append(source.substring(posStart,pos));
          output.append(newString);
          posStart = pos + lengOfold;
        }
        if(posStart < lengOfsource) {
          output.append(source.substring(posStart));
        }
        return output.toString();
      }}用上面的方法格式化一下你的字符串就行