HTML special character substitution problem HTML ESCAPTE related

Source: Internet
Author: User

This is often the case when working with Web pages, and the string data in the background contains special characters from the HTML of the Web page, such as the words on the page, "Hello World" in the background to get "Hello · World . " This is because the HTML encodes the special characters.

The problem was encountered today when I got the data from the form form submitted by the foreground. The data in the page form is the normal string "Hello World", which has been encoded into the background and has been converted into ·.

This is usually done with a simple replacement when doing a crawler, like this.

String escaped = Source.replace ("<", "&lt;"). Replace ("&", "&amp;");

But considering that there are a number of special characters for a form submission, it is tedious to list the various special characters and replace them. This string problem is also common and should be handled by a mature toolkit.

After Google, it did not disappoint me, this tool is in the Apache Commons.lang package, is the Stringescapeutils tool class.

To deal with this problem, just do the following in the background Java code.

String escaped = stringescapeutilssource.unescapehtml (source);

The value in escaped is the normal string "Hello World".


Stringescapeutils also offers a number of other methods, such as escapecsv/unescapecsv,escapejava/unescapejava,escapejavascript/ Unescapejavascript and so on, similar to the escapehtml/unescapehtml, facilitates the processing of other cases of strings. Escapehtml refers to encoding a string, unescapehtml means that the original string is displayed without encoding.


I really want to thank our predecessors for making wheels, which makes it easier for us to solve problems. We should also make better use of wheels and improve efficiency. Have the opportunity to build wheels. Also hope that one day with the development of technology, the wheel has been perfected to not need to use the wheel of our people ^_^


All right, no nonsense. The related digression, spring also provides the Htmlescape function, you can use <spring:htmlEscape> and <spring:escapeBody> tags; Add the attribute Htmlescape= "false" to the input tab of the form, complete as follows

<form:input path= "title" Htmlescape= "false"/>

Please refer to the spring related documentation for more details.


Reference Links:

Http://stackoverflow.com/questions/1265282/recommended-method-for-escaping-html-in-java


HTML special character substitution problem HTML ESCAPTE related

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.