Strip whitespace from jsp output

Viewed 93187

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?

9 Answers

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.

Please, use the trim funcion, example

fn:trim(string1)

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();
Related