1
votes

I been searching online for support but I have not found anything useful.

I have a database table with a column that contains a size constraint of 1500 characters. I am using hibernate as an ORM to a database.

My issue is with the newline character in HTML textarea. In my HTML form, I have a textarea who's maxlength is set to 1500. In the HTML textarea, a newline is perceived as ONE character only. When I submit the form via servlet the newline character is perceived as TWO characters in java.

So assuming I have filled out the textarea with 1500 characters, which contains 2 newlines in it, java sees the data passed as 1502 characters. Thus when I save the data to the database it exceeds the 1500 limit and therefor violates the database column constraints.

Sample Text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in est eu augue facilisis viverra. Phasellus eu lectus a nisi feugiat aliquet quis ut velit. Nunc vehicula neque sit amet quam interdum viverra. Morbi aliquet consequat lorem non rhoncus. Sed mauris mi, scelerisque sit amet tempus molestie, dapibus vitae quam. Proin id massa velit, commodo facilisis velit. In blandit, nibh quis auctor convallis, lectus nibh venenatis orci, quis dignissim sapien arcu eget erat. In quis diam id mauris consectetur tempor sit amet sit amet justo. Donec egestas metus eu massa vestibulum ultrices. Quisque vitae scelerisque metus. Vestibulum vehicula mi id augue condimentum pellentesque.

Suspendisse tempus turpis lobortis turpis imperdiet vulputate. Fusce fringilla lorem et ligula feugiat id sodales nisi vehicula. Praesent egestas vestibulum ante, eget consequat magna molestie sit amet. Praesent egestas pharetra augue egestas imperdiet. Suspendisse ut elit augue, sit amet ornare nulla. Morbi cursus, diam at commodo gravida, elit lacus pulvinar lectus, vitae porttitor mauris sapien sed felis. Vestibulum non iaculis erat. Phasellus pharetra arcu sed dui porta tempus. Mauris lectus velit, cursus quis facilisis a, mattis rhoncus quam. Ut nisl nulla, malesuada imperdiet dictum id, sagittis quis orci. Duis bibendum pharetra quam eu pharetra. Fusce interdum nulla vel elit bibendum non pellentesque dui egestas. Praesent sed lacus sapien. Vesbulum id arcu neque, et faucibus lorem. Fusce metus.

The gap between the paragraphs contain 2 newlines

What I need to know is how can I go about the different interpretation of the newline character from HTML to Java?

Thank for your help.

2
Maybe convert it on the server? Somehow look for a carriage return character followed by a line feed, and replace the pair with only a line feed.Elias Zamaria

2 Answers

2
votes

You can replace \r\n style line endings with \n line endings:

str = str.replaceAll("(\\r)", "");

This should leave just the \n line endings.

0
votes

The new line character in html and java are the same: \n what is possibly happening is that you are using really two new line character in your textarea

Quisque vitae scelerisque metus. Vestibulum vehicula mi id augue condimentum pellentesque.\n (1)
\n(2)
Suspendisse tempus turpis lobortis turpis imperdiet vulputate. Fusce fringilla lorem et ligula feugiat id