Replacing Newline Characters in Java and JSTL

Published on October 26, 2015 by

In this article, we will take a look at how we can replace newline characters in Java. Additionally, we will implement a custom JSTL tag so that we can replace newline characters with corresponding <br /> break lines within JSP files. Unlike PHP’s nl2br function, for instance, this is not as easy in JSTL.

Replacing or Removing Newlines in Java

First, let’s see how we can replace newline characters in plain Java. It is very important to account for different newline separators for different platforms (and when moving data between platforms). Luckily, Java’s java.util.Scanner does just that for us, and this is very convenient, because it saves us some time dealing with this ourselves. With the java.util.Scanner utility, we simply make use of its nextLine method and iterate through each line.

public final class GeneralUtils {
    private GeneralUtils() { }

    /**
     * Replaces all occurrences of newlines within a string with the HTML break line
     *
     * @param input The string to add HTML break lines to
     * @return The input string with HTML break lines instead of newlines
     */
    public static String nl2br(String input) {
        Scanner scanner = new Scanner(input);
        List lines = new ArrayList<>();

        do {
            lines.add(scanner.nextLine());
        } while (scanner.hasNextLine());

        return String.join("<br />", lines);
    }
}

I have wrapped the above method within a utility class for convenience. What the method does is to simply iterate through the lines in the input and add it to an ArrayList. After iterating through all of the lines (thus having split the input), the list elements are joined together with the <br /> break line tag, effectively replacing the newline characters.

This method replaces the newlines with HTML break lines, but you can of course modify it to replace it with whatever you would like, such as an empty string, in case you simply want to remove the newline characters. To use the utility method, simply do as below.

String split = GeneralUtils.nl2br("Some input");

Replacing Newlines with HTML Line Breaks in JSTL

Now that we have a handy utility for replacing newline characters in plain Java, let’s move on to seeing how we can accomplish this with JSTL. It is a bit tricky, because we cannot simply write a JSTL function and output the results of that, because we don’t want to leave a cross-site scripting (XSS) vulnerability behind. Therefore we should escape any markup within our data, but doing that, we will also escape any HTML line breaks. The result is that our line breaks would be displayed as <br /> rather than being rendered in the browser.

Instead, what we can do is to do things the other way around; begin by escaping any markup that we don’t want, and then replace the newlines after that. The result is a string with any markup escaped, except for line breaks that were added instead of newlines.

What we have to do, is to create a custom JSTL tag. If you are not familiar with creating custom JSTL tags, then please perform a web search, as explaining this is outside the scope of this article.

<?xml version="1.0" ?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
    <short-name>custom</short-name>

    <tag>
        <name>lineToBreakLine</name>
        <tag-class>com.codingexplained.app.tag.NewLineToBreakLine</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>value</name>
            <required>true</required>
            <type>java.lang.String</type>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>var</name>
            <required>false</required>
            <type>java.lang.String</type>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

The tag takes two parameters; the value for which we want to turn newlines into line breaks, and an optional name of a variable in which to store the result, in case we don’t want to output it directly. Below is the implementation of the above tag.

public class NewLineToBreakLine extends TagSupport {
    private static Logger logger = LogManager.getLogger(DomainNameUri.class);
    private String value;
    private String var;

    @Override
    public int doStartTag() throws JspException {
        try {
            if (this.value != null && !this.value.isEmpty()) {
                String output = GeneralUtils.nl2br(this.value);

                if (this.var != null && !this.var.isEmpty()) {
                    this.pageContext.getRequest().setAttribute(this.var, output);
                } else {
                    this.pageContext.getOut().write(output);
                }
            }
        } catch (Exception e) {
            logger.fatal(e.getMessage(), e);
        }

        return super.doStartTag();
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setVar(String var) {
        this.var = var;
    }
}

The above code is simple – especially if you are familiar with writing custom JSTL tags. We just use the utility method that we implemented in the beginning of this article, and if a variable name has been passed as an argument, then we set the result to a request attribute with that name. Otherwise we simply output the result directly, such that it will appear wherever we use the tag within a JSP file. Note that it is important that you remember to add setter methods to the tag’s instance variables.

To use our new tag within a JSP file, simply do as follows.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="custom" uri="/WEB-INF/tlds/tags.tld" %>

<h1>HTML Line Breaks Test</h1>

Test 1:
<p><custom:lineToBreakLine value="${fn:escapeXml(text)}" /></p>

Test 2:
<p><custom:lineToBreakLine value="${fn:escapeXml(text)}" var="lineBreaks" /></p>
${lineBreaks}

In the above example, we use the escapeXml JSTL function to first escape any potentially harmful markup, before passing the result on to our custom JSTL tag, which then replaces newline characters with HTML line breaks (<br />). In the first example, the result is output directly, while it is being stored within a lineBreaks variable in the second example.

Had we used a JSTL function to do the replacing instead in combination with the <c:out value=”${text}” /> tag, then we would have ended up turning our own line breaks into HTML entities!

I hope this helped. Thank you very much for reading!

Featured

Learn Java today!

Take an online course and become a Java champion!

Here is what is covered:

  • How to build complete programs using the latest version of Java
  • Writing advanced cross-platform applications
  • How to conform with best practices
  • Advanced Java concepts
  • OOP, generics, collections, JavaFX, concurrency, lambda, databases, networking, etc.
Java logo
Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

Leave a Reply

Your e-mail address will not be published.