Candid’s brain

Removing useless newlines from JSP output

What has always bothered me the most about JSP was the hundreds of useless newlines that the output produced. Take the following example code:

<html>
    <body>
        <ul>
<%
    for(int i=0; i<3; i++) {
%>
            <li><%=i%></li>
<%
    }
%>
        </ul>
    </body>
</html>

Now look at the output:

<html>
    <body>
        <ul>

            <li>0</li>


            <li>1</li>


            <li>2</li>

        </ul>
    </body>
</html>

The additional newlines are inserted because the line-breaks after the %> are preserved. Not only does this look stupid, it can also create invalid XML if the newlines occur before the <?xml declaration.

One solution would be to omit the newlines after the %>, but obviously this would make correct indentation of the HTML code inside your JSP file impossible as some lines would be preceded by two additional characters.

JSP 2.1 has introduced a trimDirectiveWhitespace parameter [source], which is included in the @page directive: <%@page trimDirectiveWhitespaces="true"%>. Look what the HTML output looks like now:

<html>
    <body>
        <ul>
<li>0</li>
<li>1</li>
<li>2</li>
</ul>
    </body>
</html>

The trimDirectiveWhitespaces parameter not only trims the newlines after the %>, but it trims all white spaces, including the indentation of the next line! I cannot imagine how anyone could have such a stupid idea.

My solution is to replace the newlines automatically in the JSP files before deploying them to the web container. This way, both the JSP file I write and the HTML code that is created look proper. To perform this task, there exists a Maven plugin called maven-replacer-plugin. This is the configuration I use in my pom.xml file:

<pluginRepositories>
    <pluginRepository>
        <id>maven-replacer-plugin reposoitory</id>
        <url>http://maven-replacer-plugin.googlecode.com/svn/release-repo</url>
    </pluginRepository>
</pluginRepositories>
<build>
    <plugins>
        <plugin>
            <groupId>com.google.code.maven-replacer-plugin</groupId>
            <artifactId>maven-replacer-plugin</artifactId>
            <version>1.3.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includes>
                    <include>target/${project.build.finalName}/**/*.jsp</include>
                </includes>
                <basedir>${basedir}</basedir>
                <replacements>
                    <replacement>
                        <token>(--%&gt;)(\n)</token>
                        <value>$2$1</value>
                    </replacement>
                    <replacement>
                        <token>(%&gt;)(\n)</token>
                        <value>$2$1</value>
                    </replacement>
                </replacements>
                <regexFlags>
                    <regexFlag>MULTILINE</regexFlag>
                </regexFlags>
            </configuration>
        </plugin>
    </plugins>
</build>

This moves the newline behind the %> before it. JSP comments (ending with --%>) are handled separately to not break them. By only moving the newline and not removing it, the line numbers in error messages are still correct. Look at the output now:

<html>
    <body>
        <ul>
            <li>0</li>
            <li>1</li>
            <li>2</li>
        </ul>
    </body>
</html>

Unfortunately, I have not yet found a way to use this together with maven-jspc-plugin, which accesses the JSP files from the source folder directly, so the replacement does not have any effect when you pre-compile your JSP files.

Update: Feel free to copy from my adventurous way of compiling the whitespace-fixed JSP files.

Filed under bugs

1 Comment

  1. Candid’s brain » Blog Archiv » Why Apache Velocity sucks



    […] how messed up the indentation is? At least it has to be said that Velocity, in contrast to JSP, is that intelligent that it does not output the newlines of those lines that only contain Velocity […]

Leave a Reply