• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Preserve line breaks in textarea

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic