119

How can I strip out extra whitespace from jsp pages' output? Is there a switch I can flip on my web.xml? Is there a Tomcat specific setting?

Seth Weiner
  • 2,999
  • 5
  • 26
  • 14

10 Answers10

183

There is a trimWhiteSpaces directive that should accomplish this,

In your JSP:

<%@ page trimDirectiveWhitespaces="true" %>

Or in the jsp-config section your web.xml (Note that this works starting from servlet specification 2.5.):

<jsp-config>
  <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <trim-directive-whitespaces>true</trim-directive-whitespaces>
  </jsp-property-group>
</jsp-config>

Unfortunately if you have a required space it might also need strip that, so you may need a non-breaking space in some locations.

jseidl
  • 13
  • 4
Rontologist
  • 3,568
  • 1
  • 20
  • 23
  • 2
    Is there a difference in terms of performance between these two options? – jlb Apr 10 '12 at 10:25
  • The trimDirectiveWhitespaces is only supported by servlet containers that support JSP 2.1 and after, or in the case or Tomcat, Tomcat 6 (and some versions e.g. Tomcat 6.0.10 don't implement it properly - don't know about the others), there's more information about trimDirectiveWhitespaces here: http://java.sun.com/developer/technicalArticles/J2EE/jsp_21/ and here http://raibledesigns.com/rd/entry/trim_spaces_in_your_jsp1 – wavetree Aug 20 '12 at 13:40
  • 3
    And in JSP custom .tag files, use <%@ tag body-content="scriptless" trimDirectiveWhitespaces="true" %>. – Thomas W Apr 03 '13 at 03:06
  • Tomcat 7 `web.xml`: trimSpaces true – Ujjwal Singh Jul 22 '14 at 20:25
  • I'm running Tomcat 8 and this does not seem to work. Is it working for anyone? – glez Oct 13 '16 at 14:34
  • I got the same issue while using JSP c:out tag to display a java bean value using tiles. That had leading and trailing whitespaces . This solution worked correctly , using page directive like this fixed the issue ! – rinilnath Jun 29 '17 at 13:10
28

If your servletcontainer doesn't support the JSP 2.1 trimDirectiveWhitespaces property, then you need to consult its JspServlet documentation for any initialization parameters. In for example Tomcat, you can configure it as well by setting trimSpaces init-param to true in for JspServlet in Tomcat's /conf/web.xml:

<init-param>
    <param-name>trimSpaces</param-name>
    <param-value>true</param-value>
</init-param>

A completely different alternative is the JTidyFilter. It not only trims whitespace, but it also formats HTML in a correct indentation.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have configured by setting trimSpaces init-param to true in /conf/web.xml but the spaces in the generated html are not trimmed. I am using Tomcat 6.0. Any ideas? – ria Apr 19 '10 at 13:10
  • 7
    @ria: Note that you need to understand that this only trims whitespace left by taglibs like JSTL and scriptlets. If you want to trim ALL whitespace from HTML, then head for a different solution. You can find a filter example here: http://balusc.blogspot.com/2007/12/whitespacefilter.html – BalusC Apr 19 '10 at 13:27
  • I have just noticed a couple snags with the code from the article b/c things on my site started showing up wrong. I copied the html source and pasted it into the w3 validator to find out that the whitespace filter is occasionally concatenating attributes. For example this... – gmustudent Mar 30 '13 at 21:59
5

The trimDirectiveWhitespaces is only supported by servlet containers that support JSP 2.1 and after, or in the case or Tomcat, Tomcat 6 (and some versions e.g. Tomcat 6.0.10 don't implement it properly - don't know about the others). There's more information about trimDirectiveWhitespaces here:

http://www.oracle.com/technetwork/articles/javaee/jsp-21-136414.html

and here

http://raibledesigns.com/rd/entry/trim_spaces_in_your_jsp1

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Simon B
  • 1,784
  • 3
  • 21
  • 26
4

If you're using tags, you can apply there too:

<%@ tag description="My Tag" trimDirectiveWhitespaces="true" %>

And on your jsp:

<%@ page trimDirectiveWhitespaces="true" %>
Andres
  • 1,090
  • 14
  • 30
4

Not directly what you're asking for, but what helps me is putting HTML comment tags in a clever way around my jsp tags, and also putting whitespace inside a servlet tag (<% %>):

${"<!--"}
<c:if test="${first}">
    <c:set var="extraClass" value="${extraClass} firstRadio"/>
</c:if>
<c:set var="first" value="${false}"/>
${"-->"}<%

%><input type="radio" id="input1" name="dayChooser" value="Tuesday"/><%
%><label for="input1" class="${extraClass}">Tuesday</label>
redolent
  • 4,159
  • 5
  • 37
  • 47
1

You can go one step further and also remove newlines (carriage returns) between the html tags at build time.

E.g. change:

<p>Hello</p>
<p>How are you?</p>

into:

<p>Hello</p><p>How are you?</p>

Do do that, use the maven-replacer-plugin and set it up in pom.xml:

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.3</version>
    <executions>
        <execution>
            <id>stripNewlines</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>replace</goal>
            </goals>
            <configuration>
                <basedir>${project.build.directory}</basedir>
                <filesToInclude>projectname/WEB-INF/jsp/**/*.jsp</filesToInclude>
                <token>&gt;\s*&lt;</token>
                <value>&gt;&lt;</value>
                <regexFlags>
                    <regexFlag>MULTILINE</regexFlag>
                </regexFlags>
            </configuration>
        </execution>
    </executions>
</plugin>

This will only modify the JSPs in the build-directory, and not touch the JSPs in your sources.

You might need to adapt the path (<filesToInclude>) where your JSPs are located in.

yglodt
  • 13,807
  • 14
  • 91
  • 127
1

Please, use the trim funcion, example

fn:trim(string1)
Jorge Santos Neill
  • 1,635
  • 13
  • 6
0

Add/edit your tomcat catalina.properties file with

org.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false

See also: https://confluence.sakaiproject.org/display/BOOT/Install+Tomcat+7

Earlz
  • 62,085
  • 98
  • 303
  • 499
0

Just a bit off the actual question, If you want to get rid of the empty lines cause by whatever you did before outputting, you can use

out.clearBuffer();
Ghostff
  • 1,407
  • 3
  • 18
  • 30
0

In web.xml add this servlet this trim param

<servlet>
                <servlet-name>jsp</servlet-name>
                <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
                <init-param>
                    <param-name>fork</param-name>
                    <param-value>false</param-value>
                </init-param>
                <init-param>
                    <param-name>xpoweredBy</param-name>
                    <param-value>false</param-value>
                </init-param>
                <init-param>
                    <param-name>trimSpaces</param-name>
                    <param-value>true</param-value>
                </init-param>
                <load-on-startup>3</load-on-startup>
            </servlet>
            <servlet-mapping>
                <servlet-name>default</servlet-name>
                <url-pattern>/</url-pattern>
            </servlet-mapping>

            <!-- The mappings for the JSP servlet -->
            <servlet-mapping>
                <servlet-name>jsp</servlet-name>
                <url-pattern>*.jsp</url-pattern>
                <url-pattern>*.jspx</url-pattern>
            </servlet-mapping>