一个简单的:
public String escapeHTMLTags( String input ) {
        //Check if the string is null or zero length -- if so, return
        //what was sent in.
        if( input == null || input.length() == 0 ) {
            return input;
        }
        //Use a StringBuffer in lieu of String concatenation -- it is
        //much more efficient this way.
        StringBuffer buf = new StringBuffer(input.length());
        char ch = ' ';
        for( int i=0; i<input.length(); i++ ) {
            ch = input.charAt(i);
            if( ch == '<' ) {
                buf.append( "&lt;" );
            }
            else if( ch == '>' ) {
                buf.append( "&rt;" );
            }
            else {
                buf.append( ch );
            }
        }
        return buf.toString();
    }

解决方案 »

  1.   

    把所有的"<"&">"转换成&lt;和&gt;
      

  2.   

    ASP中有server.htmlencode(String),不知道JSP中是否有类似的方法。
      

  3.   

    It is simple to write equivalent to Server.HTMLEncode
    FUNCTION in java but is there is any thing equivalent
    to Server.HTMLEncode in JSP.
      

  4.   

    public String replace(String con,String tag,String rep){
    int j=0;
    int i=0;
    int k=0;
    String RETU="";
    String temp=con;
    int tagc=tag.length();
    while(i<con.length()){
    if(con.substring(i).startsWith(tag)){
    temp=con.substring(j,i)+rep;
    RETU+=temp;
    i+=tagc;
    j=i;
    }
    else{i+=1;
    }
    }
    RETU+=con.substring(j);
    return RETU;
    }public String html(String s){
    String re;
    re=replace(s,"<","&lt;");
        re=replace(re,">","&gt;");
        re=replace(re,"\n","<br>");
        re=replace(re," ","&nbsp;");
        re=replace(re,"'","&#39");
        return re;
    }