Search code examples
jsptomcatwhitespace

Strip whitespace from jsp output


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?


Solution

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