This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JSP and the fly likes Keeping line breaks from a String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "Keeping line breaks from a String" Watch "Keeping line breaks from a String" New topic
Author

Keeping line breaks from a String

Arthur Colombo
Greenhorn

Joined: Apr 21, 2011
Posts: 11
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

Joined: Jan 10, 2002
Posts: 56185
    
  13

<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.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3670

And Welcome to JavaRanch!


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Arthur Colombo
Greenhorn

Joined: Apr 21, 2011
Posts: 11
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

Joined: Jan 10, 2002
Posts: 56185
    
  13

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

Joined: Apr 21, 2011
Posts: 11
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Keeping line breaks from a String
 
Similar Threads
Keep HTML tag in String
help with custom tags
ApplicationResources.properties, and the browser is rendering my <br> tags
Major alfabet soup problems: servlets+mySQL,+javascript combined
Carriage return within a text field