Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Preserve line breaks in textarea

 
Darren Wheatley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darren Wheatley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic