it depends on what you have stored. if it is already dirty, ie has possible markup especially some you might want to keep, i would use either jsoup, antisamy (getSafeHtml()), or owasp java html sanitizer, and provide it a list of allowed stuff and call it a day. let those tools do the work for you. this is the strategy that should be at play when you use stuff like tinymce because html is expected but you still need to protect it, but won't be able to encodeForHtml(). if you are only trying to preserve the new lines, then do the replace before calling encodeforhtml() but replace it with something it isn't going to encode like [br] then encode, then replace that simpler string with actual brs. I would also recommend replacing anything with 3 or more consecutive of those br's with just 2, your display will thank you.