Various escape and inversion semantics (Java) for strings using stringescapeutils

Source: Internet
Author: User

 various escape and inversion semantics (Java) for strings using StringescapeutilsCategory: Java programming ideas Guava-commons-xml-json and other tools library 2015-02-03 20:51 6290 people read reviews (1) Favorites report Stringescapeutils Usage Example Stringescapeutils use the escaped string escape tool for strings in Java stringescapeutils

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
  1. Package stringescapeutils;
  2. Import Org.apache.commons.lang.StringEscapeUtils;
  3. Public class Stringescapeutilstest {
  4. public static void Main (String args[]) {
  5. String sql="1 ' or ' 1 ' = ' 1";
  6. System.out.println ("anti-SQL injection:" +stringescapeutils.escapesql (SQL)); //anti-SQL injection
  7. System.out.println ("Escape HTML, note kanji:" +stringescapeutils.escapehtml ("<font>chen lei xing</font>")); //Escape HTML, note Kanji
  8. System.out.println ("invert semantic HTML:" +stringescapeutils.unescapehtml ("<font>chen lei xing</font>")); //Invert semantic HTML
  9. System.out.println ("turn into Unicode Encoding:" +stringescapeutils.escapejava ("Chen Lei Xing")); //Escape into Unicode encoding
  10. System.out.println ("escaped xml:" +stringescapeutils.escapexml ("<name> Chen Lei xing </name>")); //Escape XML
  11. System.out.println ("Invert semantic xml:" +stringescapeutils.unescapexml ("<name> Chen Lei </name>")); //Escape XML
  12. }
  13. }

Input Result:

[Java]View Plaincopy
    1. Anti-SQL injection:1"or "1"="1
    2. Escape HTML, note kanji: <font>chen lei xing</font>
    3. Reverse righteousness Html:<font>chen Lei xing</font>
    4. Convert to Unicode encoding: \u9648\u78ca\u5174
    5. Escape xml:<name> Chen Lei hing </name>
    6. Reversal righteousness xml:<name> Chen Lei hing </name>

Various escape and inversion semantics (Java) for strings using stringescapeutils

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.