Encode output to prevent execution of malicious scripts.
Stored XSS occurs when user input is stored and later displayed to users without proper encoding. Encoding user input in output helps prevent script execution. In Java, using libraries like OWASP Java Encoder can ensure that HTML characters are encoded before rendering user input on a webpage, thus mitigating XSS risks.