Jsoup Tutorial
Selected Reading
- jsoup - Discussion
- jsoup - Useful Resources
- jsoup - Quick Guide
- jsoup - Sanitize HTML
- jsoup - Set Text Content
- jsoup - Set HTML
- jsoup - Set Attributes
- jsoup - Working with URLs
- jsoup - Extract HTML
- jsoup - Extract Text
- jsoup - Extract Attributes
- jsoup - Using Selector Syntax
- jsoup - Using DOM Methods
- jsoup - Loading File
- jsoup - Loading URL
- jsoup - Parsing Body
- jsoup - Parsing String
- jsoup - Environment Setup
- jsoup - Overview
- jsoup - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
jsoup - Sanitize HTML
jsoup - Sanitize HTML
Following example will showcase prevention of XSS attacks or cross-site scripting attack.
Syntax
String safeHtml = Jsoup.clean(html, Safepst.basic());
Where
Jsoup − main class to parse the given HTML String.
html − Initial HTML String.
safeHtml − Cleaned HTML.
Safepst − Object to provide default configurations to safeguard html.
clean() − cleans the html using Whitepst.
Description
Jsoup object sanitizes an html using Whitepst configurations.
Example
Create the following java program using any editor of your choice in say C:/> jsoup.
JsoupTester.java
import org.jsoup.Jsoup; import org.jsoup.safety.Safepst; pubpc class JsoupTester { pubpc static void main(String[] args) { String html = "<p><a href= http://example.com/ " +" oncpck= checkData() >Link</a></p>"; System.out.println("Initial HTML: " + html); String safeHtml = Jsoup.clean(html, Safepst.basic()); System.out.println("Cleaned HTML: " +safeHtml); } }
Verify the result
Compile the class using javac compiler as follows −
C:jsoup>javac JsoupTester.java
Now run the JsoupTester to see the result.
C:jsoup>java JsoupTester
See the result.
Initial HTML: <p><a href= http://example.com/ oncpck= checkData() >Link</a></p> Cleaned HTML: <p><a href="http://example.com/" rel="nofollow">Link</a></p>Advertisements