1

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 />");
Community
  • 1
  • 1
Aishwarya Shiva
  • 3,460
  • 15
  • 58
  • 107
  • you just need to do encodeURIComponent($("#idOfTextArea")) in the request url in javascript and only get value by request.getParameter("textareaname") and save into DB –  Dec 23 '17 at 14:50

1 Answers1

3

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.

Diego Marinelli
  • 636
  • 5
  • 13
  • I agree with your point Eric but I also tried this String a=req.getParameter("topicdes").toString().replaceAll("\n","
    "); So I think its same as of ur code.
    – Aishwarya Shiva Apr 08 '12 at 11:42
  • can you show us the result of req.getParameter("topicdes").toString(); –  Apr 08 '12 at 11:58