• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Newline character doesn't render in browser

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the below code displays new line in console, but not in the browser/database? How can I resolve this?



Please assume that this code snippet is a part of the bigger code that is saving the conversation in a database.
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because newline characters are normally interpreted by HTML as spaces. The usual way to have the text start on the next line in HTML is via a <br> tag.

(If the text is enclosed in a <pre> element then newline characters are interpreted as new line instructions, but <pre> does other things which might be inconsistent with what you want the page to look like.)
 
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Likes 1 Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly, you can't send html tags in a stringbuilder, they're not being rendered eather, if you use , that's exactly how it will show up, as a string, not as an htmltag
 
Saloon Keeper
Posts: 5718
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:If I remember correctly, you can't send html tags in a stringbuilder, they're not being rendered eather, if you use , that's exactly how it will show up, as a string, not as an htmltag


It's immaterial how the HTML that gets sent to the browser is created. If HTML tags are being sent, they will be interpreted. If they show up as string then something else is at work that prevents them from being interpreted.
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked for it, but coudn't find the post anymore, but I seem to remeber I posted the same question once, I also was using a stringbuilder to build a message and at the end return it to the model.
Neather \n, \r, \n\r nor br worked
 
Tim Moores
Saloon Keeper
Posts: 5718
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again: Using or not using a StringBuilder makes no difference whatsoever. Something else must have been the reason.

Paul has already pointed out that all whitespace will be treated as a single space.
 
Saloon Keeper
Posts: 10428
223
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HTML only uses whitespace as a token delimiter. That means that any whitespace (besides spaces between words) that you want to have in the web page must be in the form of HTML tags. As has been said, line breaks are inserted into a text span with the <br /> tag. If this shows up in the web page as the literal text " <br />", then it's likely because you're trying to inject it into a template that is being processed by a template engine, which will escape anything that looks like HTML. Depending on the template language, there are different ways of injecting literal HTML.
 
Marshal
Posts: 67275
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As previously stated, newlines are just another whitespace character which HTML treats as a delimiter and will not render as anything in the output. Use HTML elements and CSS to control layout.

HTML can be built up in strings and still be interpreted as HTML.

There's lots of context missing from the original question. Why are you building HTML in a string in the first place? Is this a Java web application? If so, you should likely be building the view in a JSP. If not, how is the HTML string getting to the browser?
 
Bartender
Posts: 20938
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a general rule, you should not attempt to create HTML in server-side logic. It's far better to use some sort of View Template (for example, a JSP or a JSF xhtml) that contains the HTML and insert the variable data into that template for display.

As everyone else has said, "space" characters such as space, tab, newline and the like are treated specially in HTML. First, because all of them render simply as a blank horizontal space - and that includes newline. Secondly, because any sequence of "space" characters renders simply as a single horizontal space, whether there's 1 or 101. To actually render multiple horizontal space characters, there's something called the non-break space entity: &nbsp;, but actually that's not a very good thing to use. Spacing control is better done with layout tags such as <div> and with CSS.

OK, so much for Yet Another HTML lesson. If you're actually trying to, say, capture and display a chat conversation verbatim, there's a way to honor how text was originally spaced and lined. Use the <pre> tag to wrap it.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!