Storing TextArea data with line breaks in database

2019-02-28 08:23发布

问题:

I have a JSP page with a textarea HTML component. When a user presses enter and goes to next line while typing in textarea,and when he clicks save button, it saves the data from the text area to the database. But when I load the content of the database, the line breaks are gone. Means:

HELLO
WORLD

is displaying like

HELLO WORLD

I tried the method given here How do I replace all line breaks in a string with <br /> tags? but its not working. I also tried this one How to save user-entered line breaks from a TextArea to a database? but I am not using PHP and also the solution given here for another languages to replace "\n" with "<br />" also not working. Please anyone have any idea how to do that?

I tried this code:

String a=req.getParameter("topicdes").toString();
a.replaceAll("\n","<br />");

回答1:

The replaceAll method return a new String. Your variable a is never modified.

To solve your problem : a = a.replaceAll("\n","<br />");

In this kind of method, reading the javadoc is time saver.

Regards.