In HTML, a line break is created with the <br> tag.
✅ In a JSP page, you just write normal HTML for line breaks, because JSP ultimately outputs HTML to the browser.
Example inside JSP:
<p>First line.<br>Second line.</p>
✅ This will render:
First line.
Second line.
🛠️ If You Need a Line Break Dynamically Using Java in JSP
If you’re writing Java code inside a JSP (inside <% %>), and want to output a line break, you must print the <br> tag manually using out:
Example:
<%
out.println("First line.<br>");
out.println("Second line.");
%>
✅ Again — because out writes HTML, so you must manually insert the <br>.
🔥 If You Are Using Expression Language (EL)
If you’re using ${} to output text and it already contains newline characters (\n),
newline characters are NOT automatically converted into <br> in HTML.
HTML ignores \n!
✅ You must replace \n with <br>.
Example with JSTL:
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
${fn:replace(textWithNewlines, '\n', '<br />')}
Where textWithNewlines is a String containing line breaks (newlines).
🎯 Quick Summary:
| Case | How to Insert Line Break |
|---|---|
| Static text | Just use <br> in HTML |
Inside Java code (<% %>) | Use out.println("...<br>..."); |
Inside EL output (${}) | Use fn:replace to replace \n with <br /> |
📢 Important:
- HTML ignores
\nand\r\n— only<br>actually produces visible line breaks. - Always remember JSP generates HTML, so think in HTML when formatting output.