Java tool class is Apache's StringEscapeUtils escape tool class

Introduction This number is mainly the sharing of common ...

Introduction

This number is mainly the sharing of common key technology points and common tool classes of Java; And technology sharing of integration frameworks such as springboot+springcloud+Mybatisplus+druid+mysql+redis+swagger+maven+docker; Technology sharing of big data processing frameworks such as datax, kafka and flink. The article will be updated constantly. Code friends are welcome to pay attention, like collection and forwarding!

I hope all code friends can click to pay attention and make 1000 powder. Some video tutorials will be recorded later, combining graphics and video, such as Book Introduction website system, rush purchase system, big data middle desk system, etc. Technology is the favorite of program apes. Code friends rush

If the coder thinks the code is too long, he can quickly scan it from beginning to end and understand it. If you find it useful, you can forward it for collection in case of need.

Text:

Apache's StringEscapeUtils escape tool class is mainly used to escape html, json, xml, js, etc. There are not many specific usage scenarios in the project, so few people may know about them.

Stringescape utils escape example

import org.apache.commons.text.StringEscapeUtils; import org.junit.Test; ​ public class StringEscapeUtilsTest { ​ @Test public void test() { // Escape html script and anti escape html script String inputText = "<input type=\"button\" value=\"Point me\"/>"; String s1 = StringEscapeUtils.escapeHtml4(inputText); System.out.println(s1); String s2 = StringEscapeUtils.unescapeHtml4(s1); System.out.println(s2); ​ // Escape js script and anti escape js script String s3 = StringEscapeUtils.escapeEcmaScript("<script>alert('Point me')<script>"); System.out.println(s3); String s4 = StringEscapeUtils.unescapeEcmaScript(s3); System.out.println(s4); ​ // Escaping strings to unicode encoding and from Unicode encoding to strings String s5 = StringEscapeUtils.escapeJava("abc Don't order me"); System.out.println(s5); String s6 = StringEscapeUtils.unescapeJava(s5); System.out.println(s6); ​ // Escape XML and anti escape XML String s7 = StringEscapeUtils.escapeXml11("<name>Zhang San</name>"); System.out.println(s7); String s8 = StringEscapeUtils.unescapeXml(s7); System.out.println(s8); } }

Other methods:

Tool source code:

You can directly import the jar package of the tool class

<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.1</version> </dependency>

Tool classes with the same functions as those used in this article:

  • org.apache.commons.lang.StringEscapeUtils is the same as the org.apache.commons.text.StringEscapeUtils tool class, but Apache annotates it as outdated. It also has a powerful method to escape sql [escapeql], but it is recommended to use a new package org.apache.commons.text.StringEscapeUtils;

    <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency>

  • org.springframework.web.util.HtmlUtils, a tool class of spring, mainly for html. You can learn about it;

    <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.6</version> </dependency>

  • groovy.json.StringEscapeUtils has the same functions as org.apache.commons.text.StringEscapeUtils. Generally, this package is not used for projects, but you can learn about it; If you can use groovy scripts, you should use more. Groovy XXX has many tool classes, but I haven't used them. If you are interested, you can explore them.

    <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>3.0.9</version> <type>pom</type> </dependency>

I have been coding for more than ten years and have accumulated some tool classes in the project. Many tool classes are used in every project and are very practical. Most of them are encapsulated by me. Some tool classes are encapsulated by colleagues. Some tool classes don't remember whether they are ctrl+c or encapsulated by themselves. Now I will summarize most of the tool classes in the project and share them with you when I am free. If the code involved in the article has infringement, please notify me for handling.

The plan is to sort out the tools first. As the saying goes, if you want to do well, you must first sharpen the tools. In the project, whether it is an ordinary single project, a multi module maven project or a distributed microservice, some functional modules can be reused, and the tool module is one of them.

16 November 2021, 18:05 | Views: 6499

Add new comment

For adding a comment, please log in
or create account

0 comments