I have a HttpServletRequest that receives "String myString" from a HTML - textarea. The issue I have is that everytime I write a text in the textarea, for every new line, "String myString" receives a "\r\n" instead of just a "\n". See my code below.
EDIT: I included sample function readText() on HTML file. There, data variable gets new lines as just as "\n". It is only when receiving it from HttpServletRequest when it becomes "\r\n". The thing is that I really need it to be just "\n", as I should not get any extra bytes than in the original text.
How can I fix this? Is it because of the charset UTF-8 at HTML header?
Thanks a mil,
MyServlet.html
<meta http-equiv="content-type" content="text/javascript; charset=UTF-8">
// [...]
<form action="servlet" method="POST" name="formIn">
<textarea name="originalScript" COLS=50 ROWS=25></textarea>
</form>
<script type="text/javascript" charset="UTF-8">
function readText() {
var s = document.formIn.originalScript.value;
var data = (s + "").split("");
};
</script>
MyServlet.java
public class MyServlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
String myString=req.getParameter("originalScript");
}
}
This is not dependent on the character encoding used. This is dependent on the client platform used. When the client uses the Windows operating system, then every newline is represented by \r\n (CR+LF, Carriage Return and Line Feed). When the client uses an Unix based operating system such as Linux, Max OS X, FreeBSD, BeOS, etc, then every newline is represented by \n (LF, Line Feed). There were also platforms which use \r or \n\r, such as Commodore, Apple II, Acorn, etc, but those platforms are considered ancient and not likely in use anymore these days.
Note that the \n shows just fine as a fullworthy linebreak in Windows and that \r\n should also just show fine as a linebreak in Unix. That Windows uses \r\n is merely due to historical reasons and that Windows still uses it is due to backwards compatibility reasons.
You, as Java web developer, do not necessarily need to worry about this. The server just retrieves exactly the data whatever the client has sent to it. When redisplaying the client data, the both newline forms should display just fine in most if not all modern platforms.
If you're having problems with it, then you're probably doing things the wrong way. For example, perhaps you're splitting on \n only to get an array of lines which causes the \r being untouched. In such case you'd better be using BufferedReader instead which will handle both the \n and \r\n transparently in the readLine() method.
E.g.
BufferedReader reader = new BufferedReader(new StringReader(string));
List<String> lines = new ArrayList<String>();
for (String line = null; (line = reader.readLine()) != null;) {
lines.add(line);
}
// ...
Or when you're string-replacing \n by <br/> to get the line breaks to show up in the HTML markup, then you could also consider to use CSS white-space: pre; instead on the parent HTML element containing the text.
\n and \r\nUnrelated to the concrete problem, your <meta> tag is invalid for a HTML page. It should be text/html. Also, when the page is been served by a HTTP request, the Content-Type response header would override any <meta http-equiv="content-type">. The <meta> tags are only used when the page is viewed from local disk file system. To learn more about character encoding, refer this article (again, this is unrelated to your concrete problem).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With