The Apache Toolkit Common-lang has a very useful tool class for handling strings, one of which is stringescapeutils, a tool class that is added to the 2.3 version above, and it can be easily html,xml with The escape and reversal semantics of Java, but also the key string can be processed to prevent SQL injection, but it seems like common-lang3.0 after I looked like there is no way to deal with SQL statements, if you want to use the premise when the corresponding jar package, the following is its part of the method:
Its methods, all static, directly with the class call, the following code to see their usage and effect, a glance at the bottom:
Package Stringescapeutils;import Org.apache.commons.lang.stringescapeutils;public class Stringescapeutilstest { public static void Main (string args[]) {string sql= "1 ' or ' 1 ' = ' 1"; SYSTEM.OUT.PRINTLN ("Anti-SQL injection:" +stringescapeutils.escapesql (SQL));//anti-SQL injection SYSTEM.OUT.PRINTLN ("Escape HTML, Note kanji:" + Stringescapeutils.escapehtml ("<font>chen lei xing</font>")); Escape HTML, note the kanji System.out.println ("Invert HTML:" +stringescapeutils.unescapehtml ("<font>chen Lei Xing</font > "));//Invert HTMLSystem.out.println (" Turn into Unicode Encoding: "+stringescapeutils.escapejava (" Chen Lei Xing ")); Escaped into Unicode encoded SYSTEM.OUT.PRINTLN ("Escaped xml:" +stringescapeutils.escapexml ("<name> Chen Lei </name>")); Escape xmlSystem.out.println ("Inverse literal xml:" +stringescapeutils.unescapexml ("<name> Chen Lei </name>")); Escape XML}}
Input Result:
Anti-SQL injection: 1 "or" 1 "=" 1 escaped HTML, note Chinese characters: <font>chen lei xing</font> reversal righteousness html:<font>chen lei xing< /FONT> to Unicode Encoding: \u9648\u78ca\u5174 escape xml:<name> Chen Lei hing </name> Reverse righteousness xml:<name> Chen Lei Xing </name>
Reprint please indicate-java my life (Chen Leixing) Source: http://blog.csdn.net/chenleixing/article/details/43456987
Use the Stringescapeutils tool class to perform various escape and reversal semantics for strings (Java)