Storing TextArea data with line breaks in database

2019-02-28 08:29发布

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条回答
beautiful°
2楼-- · 2019-02-28 09:06

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.

查看更多
登录 后发表回答