HTML encoding converts special characters to HTML entities, preventing script execution.
Cross-Site Scripting (XSS) attacks occur when an attacker injects malicious scripts into web pages viewed by other users. HTML encoding is a defense mechanism that converts characters like , and & into their respective HTML entities (<, >, &) so that they are displayed as text rather than executed as code. For example, in a Java web application, libraries like OWASP's Java Encoder can be used to safely encode user-generated content before rendering it in the browser.