Original: http://blog.csdn.net/chenleixing/article/details/43456987
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:
[Java]View PlainCopy
- 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 Kanji
- System.out.println ("invert semantic HTML:" +stringescapeutils.unescapehtml ("<font>chen lei xing</font>")); //Invert semantic HTML
- System.out.println ("turn into Unicode Encoding:" +stringescapeutils.escapejava ("Chen Lei Xing")); //Escape into Unicode encoding
- System.out.println ("escaped xml:" +stringescapeutils.escapexml ("<name> Chen Lei xing </name>")); //Escape XML
- System.out.println ("Invert semantic xml:" +stringescapeutils.unescapexml ("<name> Chen Lei </name>")); //Escape XML
- }
- }
Input Result:
[Java]View PlainCopy
- Anti-SQL injection:1"or "1"="1
- Escape HTML, note kanji: <font>chen lei xing</font>
- Reverse righteousness Html:<font>chen Lei xing</font>
- Convert to Unicode encoding: \u9648\u78ca\u5174
- Escape xml:<name> Chen Lei hing </name>
- Reversal righteousness xml:<name> Chen Lei hing </name>
Various escape and inversion semantics (Java) for strings using stringescapeutils