This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I'm getting text from a database. It can range from simple sentences to several paragraphs. What I want to do is to retain the paragraph breaks and spaces when I display it in the JSP. Would anybody how to go about this?
Indeed, line breaks in HTML are to be represented by the <br> element. You can use JSTL's fn:replace for this or write a simple EL function for this like nl2br().
Do not only replace \n, it is platform dependent. Windows for instance uses \r\n. You need to replace \r\n then. Call System.getProperty("line.separator") to find out which is correct. Doing so in an EL function makes it more robust.
By the way, you do not necessarily need regex for this. It's a simple character-by-character replace, not a pattern-by-character replace. The \r and \n itself already represents the desired characters. Save yourself from the backslash-escaping headache and minor performance drop when using regex for this.