aspose file tools*
The moose likes Servlets and the fly likes Preserve line breaks in textarea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Preserve line breaks in textarea" Watch "Preserve line breaks in textarea" New topic
Author

Preserve line breaks in textarea

Darren Wheatley
Greenhorn

Joined: Nov 27, 2003
Posts: 11
Hi,

I have a form in a JSP that posts to a Servlet. The servlet extracts all of the field values in the form, uses StringBuffer to make a big string out of them all, and then uses that String as the body of an email.

Everything is working fine except where the user inputs more than one line in a <textarea>. The line breaks are being stripped out of the field when it is converted to a String.

So, instead of seeing this in my email:

Line1
Line2
Line3

- I get this:

Line1 Line2 Line3

Is there a way to preserve the line breaks?

Thanks

D.
[ October 06, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61650
    
  67

Nothing to do with JSP, so I've moved this off to the Servlets forum.

Some details regarding how you are "converting" will be necessary.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Darren Wheatley
Greenhorn

Joined: Nov 27, 2003
Posts: 11
Hi,

My forms posts to itself, populating a bean call "QuestionnaireBean" at page load using:

<jsp:useBean id="questionnaireBean" class="com.djwheatley.QuestionnaireBean" scope="session"/>
<jsp:setProperty name="questionnaireBean" property="*" />

If the bean is successfully validated the user is redirected to MailServlet.

The questionnaireBean is then pulled from the session inside the MailServlet using:

QuestionnaireBean qBean = (QuestionnaireBean) session.getAttribute("questionnaireBean");

I then populate a StringBuffer "emailBody" like this:

emailBody = new StringBuffer();
emailBody.append("The following message was entered in the questionnaire form:").append("\n\n\n\n");

emailBody.append("\n============================================================\n");
emailBody.append("Contact information\n");
emailBody.append("============================================================\n\n");

emailBody.append("Date: ").append(qBean.getDateSent()).append("\n\n");
emailBody.append("Name: ").append(qBean.getName()).append("\n\n");
emailBody.append("AddressLine1: \n\n").append(qBean.getAddressLine1()).append("\n\n");
emailBody.append("AddressLine2: \n\n").append(qBean.getAddressLine2()).append("\n\n");


However, when I use "emailBody.append" to add the value of a <textarea> field the field gets added with the linebreaks removed.

This means that when I should be getting this:

Line1
Line2
Line3

- I actually get this in the StringBuffer:

Line1 Line2 Line3

Can anyone tell me how to preserve the linebreaks in <textarea> fields when adding to a StringBuffer please?

Thanks

D.
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
However, when I use "emailBody.append" to add the value of a <textarea> field the field gets added with the linebreaks removed.

Are you sure there are really newlines in the text from the textarea? Are you sure the text isn't just wrapping? Is the textarea's col attribute assigned a small value?

As a test, you could immediately returning a String containing the textarea text to the browser replacing newlines with <br />'s. Or, it might be easier to write the text to a file and examine the file for the newlines.


[ October 06, 2006: Message edited by: sven studde ]
[ October 06, 2006: Message edited by: sven studde ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Preserve line breaks in textarea