3

My java webapp fetches content from a textarea, and e-mails the same.

The problem I'm facing is that the newline character in the textarea message is not preserved when reading the same using

request.getParameter("message");

Any clues how it can be tackled?

TIA.

EDIT:

The content in the textarea is:
abcd
abcd

CODE:

String message = request.getParameter("message");

        System.out.println("index loc for message "+message+" using \\r\\n : "+message.indexOf("\r\n"));
        System.out.println("index loc for message "+message+" using \\n : "+message.indexOf("\n"));
        System.out.println("index loc for message "+message+" using \\r : "+message.indexOf("\r"));
        System.out.println("index loc for message "+message+" using \\n\\r : "+message.indexOf("\n\r"));

OUTPUT:

index loc for message asdfasdf using \r\n : -1
index loc for message asdfasdf using \n : -1
index loc for message asdfasdf using \r : -1
index loc for message asdfasdf using \n\r : -1

James
  • 93
  • 3
  • 4
  • 12
  • 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:49

4 Answers4

3

That completely depends on how you're redisplaying it.

It sounds like that you're redisplaying it in HTML. "Raw" newlines are not part of HTML markup. Do a rightclick, View Page Source in webbrowser. You'll see linebreaks over all place. Usually before and/or after HTML tags.

In order to visually present linebreaks in the HTML presentation, you should actually be using <br> tags. You can replace newlines by <br> strings as below:

message = message.replace("\n", "<br>");

This is only sensitive to XSS attack holes if the message is an user-controlled variable, because you have to present it unescaped in JSP (i.e. without <c:out>) in order to get <br> to work. You thus need to make sure that the message variable is sanitized beforehand.

Alternatively, you can also set CSS white-space property there where you're redisplaying the message to pre. If you'd like to wrap lines inside the context of a block element, then set pre-wrap. Or if you'd like to collapse spaces and tabs as well, then set pre-line.

<div id="message"><c:out value="${message}" /></div>
#message {
    white-space: pre-line;
}

This will display the text preformatted (as a textarea by default does).

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • We know he's shipping the text out by e-mail, but we don't know how the text is being displayed once it arrives. If it's indeed being displayed as HTML, there's a lot of work to be done indeed. Just maintaining line breaks would be just a small part of the problem, as the users could maliciously or accidentally introduce all kinds of harmful tags into the text. – Carl Smotricz Jul 15 '10 at 12:43
  • Nice answer. you mean `replaceAll`? – mdma Jul 15 '10 at 12:44
  • @mdma: No, certainly not. Regex is overkill for a simple char(s)-by-char(s) replace. – BalusC Jul 15 '10 at 12:46
  • @BalusC. I'd not noticed that replace() also takes a CharSequence, and it's only been in there since 1.5 so I've only had 4 or so years to notice... ;-) – mdma Jul 15 '10 at 12:54
  • @mdma: It's indeed unfortunately one of the least known methods in `String`. I've even ever downvotes for that here at SO because a sad nitpicker thought that it wouldn't compile! – BalusC Jul 15 '10 at 12:58
  • @balusc which is not really surprising, as google often points you to 1.4 javadocs when you google a java class, e.g. http://www.google.com/search?hl=en&q=java.util.hashmap – Sean Patrick Floyd Jul 15 '10 at 13:38
  • 1
    @sean: That's exactly why I google in this style: [hashmap /6 site:oracle.com](http://www.google.com/search?q=hashmap+%2F6+site%3Aoracle.com) – BalusC Jul 15 '10 at 13:47
  • I'm displaying it as plain text. – James Jul 16 '10 at 05:32
  • @BalusC: Firstly, thanks for the effort you've put in. I tried the code, and it's working perfect. But in my actual implementation, it's not. Could it be because I'm sending the parameters using javascript?(I'm using ajax). – James Jul 20 '10 at 05:37
1

Searching the ASCII codes i found that the new line is not defined like the often \n, instead is defined like \r\n.

Regards.

Estuardolh
  • 111
  • 1
  • 6
1

You need to encodeURIComponent() before submitting the form and decodeURIComponent() on the server side.

RAS
  • 8,100
  • 16
  • 64
  • 86
Anon
  • 11
  • 1
1

Two possible problems:

  • The text in the textarea is word wrapped and doesn't really have any newlines.

  • The String you get with getParameter() contains newlines (\n) but no carriage returns (\r) as expected by many email programs.

As a first step, I'd try dumping the retrieved String in a way you can check for this. You could write to a file and use od or a hex editor to look at the file, for example.

If it turns out you're simply missing CRs, you could do some simple regexp-based replacement on the string to fix that.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
  • The getParameter() definitely doesn't contain newlines(\n). Because the string concatenated to it, and having newlines(\n), are getting displayed properly, but not the message obtained from textarea. – James Jul 16 '10 at 05:30
  • Hmm. Maybe CRs? I find it hard to believe a browser would not send *any* line terminator. – Carl Smotricz Jul 16 '10 at 07:18
  • The browser does in fact. When I display the value from javascript using `alert`, it does display the content in respective lines – James Jul 16 '10 at 08:18
  • OK, I give up. The only way I'd see to fix your problem would be for me to set up a project to duplicate some of what you're doing, and that's more work than I'm willing to do. Hopefully you or someone else manages to come up with a bright insight eventually, good luck! – Carl Smotricz Jul 16 '10 at 08:35