Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Keeping line breaks from a String  RSS feed

 
Arthur Colombo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've tried a few searches both here and in the servlet session, but to no avail.
What I want is to print a String that comes from a Servlet in a JSP page through a JSTL command. To do this, I'm currently setting an attribute with said String and printing it with c: out.

The problem is, being a string, the \n line breaks don't work, and trying to add <br> instead doesn't add a line break when viewing in the JSP either, instead printing it.

So, I'd like to know what I should use to create line breaks in a String that'll persist and correctly show in a JSP page with c: out.

Currently, its something similar to this

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<br> is the correct way as line breaks are invisible whitespace to an HTML page.

The reason that isn't working for you is that you are using <c:out> incorrectly. Look up the documentation for the tag to see what you are doing wrong.
 
Vijitha Kumara
Bartender
Posts: 3973
24
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to JavaRanch!
 
Arthur Colombo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the documentation points out:
escapeXml - Determines whether characters <,>,&,'," in the resulting string should be converted to their corresponding character entity codes. Default value is true.

Therefore:

Thanks for the help
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear in mind that if the string you are emitting with escapeXml enabled comes from a user, you are opening yourself up for injection attacks. Use this technique only for trusted data.
 
Arthur Colombo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The string which I am actually using in my project is created in the servlet and kept there, so that should pose no risk for the moment. Still, good to know that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!