Use the Stringescapeutils tool class to perform various escape and reversal semantics for strings (Java)

Source: Internet
Author: User
Tags key string

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.