tags:

views:

72

answers:

2

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

A: 

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. You should basically replace each newline by HTML <br> element. This can be done as follows:

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

Alternatively you can also set CSS white-space property to pre there where you're redisplaying the message:

#message {
    white-space: pre;
}

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


Update: so, there fails something seriously with sending the newlines as parameters? That's odd. Please let us know which webbrowser you're using (and which you've all tried that exposes the same behaviour) and which platform you're running (Windows? Mac? Etc). Please try the following JSP snippet unchanged:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <form>
            <textarea name="textarea"></textarea>
            <input type="submit">
        </form>
        <p>Chars: <pre>${param.textarea}</pre>
        <p>Bytes: <pre><%
            if (request.getParameter("textarea") != null) {
                for (byte b : request.getParameter("textarea").getBytes()) {
                     out.print(String.format("%04x ", b));
                }
            } 
        %></pre>
    </body>
</html>

Enter something like

abcd
abcd
in the textarea and press submit. Please let us know what exactly you see in the browser address bar and in the chars and bytes result. In my FF 3.6.6 on Windows XP I see the following in browser address bar:

http://localhost:8080/playground/test.jsp?textarea=abcd%0D%0Aabcd

and the following in the result:

Chars:

abcd
abcd

Bytes:

0061 0062 0063 0064 000d 000a 0061 0062 0063 0064 
BalusC
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
Nice answer. you mean `replaceAll`?
mdma
@mdma: No, certainly not. Regex is overkill for a simple char(s)-by-char(s) replace.
BalusC
@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
@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
seanizer
@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
I'm displaying it as plain text.
James
@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
+1  A: 

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
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
Hmm. Maybe CRs? I find it hard to believe a browser would not send *any* line terminator.
Carl Smotricz
The browser does in fact. When I display the value from javascript using `alert`, it does display the content in respective lines
James
Edited the post.
James
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