Recommended method for escaping HTML in Java

前端 未结 11 1145
南旧
南旧 2020-11-22 04:30

Is there a recommended way to escape <, >, \" and & characters when outputting HTML in plain Java code? (Other

11条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 04:56

    There is a newer version of the Apache Commons Lang library and it uses a different package name (org.apache.commons.lang3). The StringEscapeUtils now has different static methods for escaping different types of documents (http://commons.apache.org/proper/commons-lang/javadocs/api-3.0/index.html). So to escape HTML version 4.0 string:

    import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;
    
    String output = escapeHtml4("The less than sign (<) and ampersand (&) must be escaped before using them in HTML");
    

提交回复
热议问题