1

Input to textarea :(These 3 lines)

<textarea id="preM" rows="3" cols="10" style="resize: none; width:100%;">
</textarea><br/>

This is example of textarea
This is new Line
Hello All

In JSP page I am retrieving the value of Text Area

String variableString = request.getParameter("preM");
System.out.println(variableString);

Output is coming as:

This is example of textarea This is new Line Hello All

Expected output:

This is example of textarea
This is new Line
Hello All

I have already tried

StringBuffer preM = new StringBuffer(request.getParameter("preM"));
int loc = (new String(preM)).indexOf('\n');
while(loc > 0){
  text.replace(loc, loc+1, "<BR>");
  loc = (new String(preM)).indexOf('\n');
}
out.println(preM);

But getting loc=-1.

Tom
  • 16,842
  • 17
  • 45
  • 54
  • Instead of `id="preM"` you should use `name="preM"` otherwise `String variableString = request.getParameter("preM");` will print NULL – Shaggy Sep 14 '16 at 12:10

1 Answers1

0

The Best way to do that is to wrap the text into a BufferedReader, it can be used to iterate over the lines, and then use a PrintWriter to write each line out to a file using the platform-specific newline.

Please refer this SO : Strings written to file do not preserve line breaks

There are 2 methods mentioned here, personally to save memory & to perform well over larger strings I would opt the Bufferedreader way.

Community
  • 1
  • 1
Nikhil Nanjappa
  • 6,454
  • 3
  • 28
  • 44