Missing newlines when submitting a TextArea content

Y

yaneeve.shekel

Hello all!
I wish to submit a textarea from an html form to the server. In the
process of submitting by post all the newlines get stripped away and
the servlet receives a string with no newlines whatsoever. What should
I do to retain (or maybe reproduce) those missing newlines?
Thanks in advance!
 
O

Oliver Wong

Hello all!
I wish to submit a textarea from an html form to the server. In the
process of submitting by post all the newlines get stripped away and
the servlet receives a string with no newlines whatsoever. What should
I do to retain (or maybe reproduce) those missing newlines?
Thanks in advance!

What leads you to believe that the newlines got stripped? Could it be
that the newlines are present, but when you try to display them, the
client's browser chooses to strip them? You can check against this by using
the "view source" functionality in the webbrowser to check the actual output
emitted by your servlet.

- Oliver
 
S

Simon Brooke

in message <[email protected]>,
Hello all!
I wish to submit a textarea from an html form to the server. In the
process of submitting by post all the newlines get stripped away and
the servlet receives a string with no newlines whatsoever. What should
I do to retain (or maybe reproduce) those missing newlines?
Thanks in advance!

This is an HTML question, not a Java question. Look at the 'wrap' attribute
of the 'textarea' tag. It's not in any of the HTML specs but it is widely
implemented by mainstream browsers, and takes the values 'hard' (newlines
are preserved), 'soft' (content is wrapped but newlines are not preserved)
and 'off' (no wordwrap happens, newlines are preserved).
 
Y

yaneeve.shekel

I wish to thank you all for your input, but the problem had been
solved.
Let me explain the problem a bit better and then go into the solution:
The problem had been that when using the following function when
submitting the contents of a text area to a servlet the content on the
servlet side got all new lines stripped:

function submitByPost(url, contentType)
{
// Activate the post Method
var targetXmlHTTPObject=new ActiveXObject("Microsoft.XMLHTTP");
....
targetXmlHTTPObject.Open("POST", url ,false);
targetXmlHTTPObject.Send();
....
}

If the content of the textarea had been, lets say:
Blah
is
Blah

The parameter off the HttpServletRequest would be:
BlahisBlah

The solution:
Before using the submitByPost function above the parameter holding the
value of the textarea (which is part of the url function argument) is
parsed and has all newlines (\r\n)
replaced by the encoding %0D%0A (which are the hexadecimal equivalents
that are escaped with the percent sign) thus:

var text = innerText.replace(/\r\n/g, "%0D%0A");

where innerText holds the 'before' value of the textarea.

Magically when retrieving the parameter value off the request yields a
string with newlines in the proper places.

The only question that remains is why I had to do this parsing
manually???
 
S

Simon Brooke

in message <[email protected]>,
I wish to thank you all for your input, but the problem had been
solved.
Let me explain the problem a bit better and then go into the solution:
The problem had been that when using the following function when
submitting the contents of a text area to a servlet the content on the
servlet side got all new lines stripped:

function submitByPost(url, contentType)
{
// Activate the post Method
var targetXmlHTTPObject=new ActiveXObject("Microsoft.XMLHTTP");
...
targetXmlHTTPObject.Open("POST", url ,false);
targetXmlHTTPObject.Send();
...
}

That isn't Java, it's ECMAScript. If you ask questions in completely the
wrong group, it isn't surprising you get the wrong answer. There are no
function definitions in Java (at least, there are, but they aren't called
that).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top