Trim Spaces in your JSP's HTML
One of the annoying things about JSPs is all of the dynamic (non-rendered) parts of the page still produce line breaks. This means that if you do a view-source, you'll likely see large blocks of whitespace.
The good news is you can get rid of this whitespace if you're using Tomcat 5.5.x. Just locate the "jsp" servlet in $CATALINA_HOME/conf/web.xml and add the following <init-param>:
<init-param> <param-name>trimSpaces</param-name> <param-value>true</param-value> </init-param>
I tested it and it works great. This begs the question - why isn't this on by default? Source: Struts Mailing List.
Update: JSP 2.1 adds the ability to trim whitespaces.
On an older version of browsers, whitespaces affects the way a table is rendered. Tidying the HTML might actually cause difference in layout.
My question is why Tomcat 5.5 has the feature? What's the reason that pushes it?
Posted by Jason Barker on March 24, 2005 at 06:27 AM MST #
This works in tomcat 5.0.x too. But it has one disadvantage - it removes all whitespace between two JSP tags, even when you might one one space. For example, something like <c:out value="Hello"/> <c:out value="${name}"/> will now say, "HelloMatt".
It's the thought of searching through all my JSPs for these types of problems which has put me off using it.
Oh, the jasper ant task has a flag to do the same thing if you pre-compile your JSPs...
Posted by Kris on March 24, 2005 at 07:08 AM MST #
Darn - should have escaped my XML. That should have read:
Something like <c:out value="Hello"/> <c:out value="${name}/> will now say, "HelloMatt"
Posted by Kris on March 24, 2005 at 07:09 AM MST #
Posted by Larry Williams on March 24, 2005 at 07:59 AM MST #
if you want to get rid of really ALL whitespaces (not just between directives) try this in your build file:
this will reduce your pages from 10% to 50%, depending on the content (big savings for tables with many rows)
Posted by Yann Cebron on March 24, 2005 at 08:54 AM MST #
<quote>"I've been wondering why the JSP compiler creates whitespace"<quote>
The JSP compiler doesn't *create* the whitespace. The whitespace already exists in the source document and the JSP compiler just doesn't remove it (Why would it? The whitespace is outside any JSP tags). If you remove the line breaks between JSP tags then there will be none in the generated HTML.
Not this
But this
Of course then your JSP is ugly and hard to read. Pick which one bugs you least or post-process the generated html, which I'm guessing is what the Tomcat switch enables.
Posted by Steve Raeburn on March 24, 2005 at 08:12 PM MST #
Posted by Michael Slattery on March 25, 2005 at 02:12 PM MST #
Posted by Charles Havener on March 25, 2005 at 09:09 PM MST #
Posted by funkman on March 27, 2005 at 01:11 AM MST #
Posted by Michael Musson on April 05, 2005 at 05:01 PM MDT #
A previous comment stated that the whitespace already exists in the document. This is not correct because if I use
<code> <c:set var="baseurl" value="${pageContext.request.scheme}://${pageContext.request.serverName}" /> </code>
And then later in my page use
<code> <param name="baseurl" value="<c:out value="${baseurl}" />" /> </code>
The code is generated as
<code> <param name="baseurl" value=" http://myserver" /> </code>
You can see that even though there is no whitespace before the c:out tag in the second fragment, but the final output has an empty space where the c:out tag begins.
Posted by SmK on June 26, 2005 at 06:50 PM MDT #
Posted by Noruas on March 14, 2006 at 01:26 PM MST #
Posted by Dan Moore on June 26, 2006 at 08:25 PM MDT #
Posted by Patrick O'Sullivan on August 23, 2006 at 12:20 PM MDT #
Posted by Jason Andersen on August 26, 2006 at 05:24 AM MDT #
Posted by todd hodes on September 13, 2006 at 02:07 AM MDT #
Posted by Viz on November 06, 2006 at 02:10 PM MST #
Posted by zzAA on May 23, 2007 at 07:42 AM MDT #
Posted by Nataraj on May 31, 2007 at 06:16 AM MDT #
Hey guys,
The solution provided below works great too!
1. Keep trimFlt.jar in WEB-INF\lib folder
Download this jar from the reference link provided at the end.
2. Add following entries in web.xml
To remove other HTML comments the filter parameter can be changed to -
The performance impact of this fix is yet to be analysed.
Reference that I used - http://www.servletsuite.com/servlets/trimflt.htm
Posted by Ashish Nair on July 03, 2007 at 09:43 AM MDT #
Posted by Ashish Nair on July 03, 2007 at 09:47 AM MDT #
Posted by Sanath Kodikara on July 11, 2007 at 06:41 AM MDT #
Posted by Matt Raible on July 11, 2007 at 06:45 AM MDT #
Posted by Raible Designs on August 01, 2007 at 11:02 PM MDT #
Posted by Balakumar on October 25, 2007 at 10:11 AM MDT #
The trimFlt.jar mentioned above works great for standard HTML streams, but we're having problems getting it filter XHTML-MP for some reason. Is there any deeper documentation and/or any way to contact the developer? The servletsuite site is rather content-free, and I'd probably purchase the "commercial license" for $49, but only if I knew that buying it would give me more direct access to the developer and/or the code itself.
Any ideas?
Posted by Ken Scott on November 06, 2007 at 10:08 AM MST #
Posted by Mim on December 04, 2007 at 06:17 AM MST #
Posted by David Truong on January 30, 2008 at 07:52 PM MST #
Posted by jess on February 20, 2008 at 04:23 PM MST #
Posted by Sam Allen on April 04, 2008 at 10:55 AM MDT #
Posted by leoj on May 12, 2008 at 09:22 AM MDT #
Actually, IMHO this fact doen't matter at all for HTML, CSS and Javascript contents IF YOU CONFIGURE YOUR SERVER TO SERVE COMPRESSED CONTENTS for the textual mime-types (you can see tons of articles googling, for example a post written in Spanish in my blog: http://serverperformance.blogspot.com/2008/05/compresin-http.html).
I mean, patterns like tons of spaces or tabs have great compression ratios with gzip. For example I had an unoptimized JSP which sent to the browser 5,8 KBytes of html text. When eliminating carriage returns, spaces and tabs, I got a 3,5 KBytes content (hey, thats good). But when compressing the contents, in the first case 1,2 KBytes were sent though the network, and 1,1 KBytes in the second case.
So the actual difference isn't enough to worth it, at least in the 99% of the cases.
P.S: This comment is valid for HTML, Javascript and CSS contents, but surely no for JSON contents in response to AJAX requests because the JSON parser is a Javascript code and in the optimized case has to do more work for parsing it...
My 2 cents.
Regards
Posted by Server Performance on October 09, 2008 at 09:48 AM MDT #
http://java.sun.com/developer/technicalArticles/J2EE/jsp_21/
Posted by Will on January 31, 2010 at 04:14 PM MST #
Posted by Will on January 31, 2010 at 04:15 PM MST #
trimDirectiveWhitespaces
Does anyone know of any container in which that directive actually works as it should?
It does seem to do something, the problem is not as bad as it is without, but it definitely does not do what is promised in the article: http://java.sun.com/developer/technicalArticles/J2EE/jsp_21/
Specifically the forEach example, I tried this and it leaves blank lines in the output where the open and close tags of the forEach were in the template.
Posted by Stijn de Witt on August 29, 2010 at 08:34 PM MDT #
Posted by Matt Raible on September 30, 2010 at 09:00 PM MDT #
Hi,
I am using the trimfit jar, I just want to trim one jsp, for that i am giving the entry in my web.xml as
But its not trimming the abc.jsp. I tried */abc.jsp and /abc.jsp too. But my abc.jsp is not getting trimmed.
But if i give the pattern as *.jsp, it works fine.
Anybody faced such similar issue?
Posted by Harsha on April 11, 2012 at 12:59 PM MDT #
Just in case anyone else is still stuck on old jsp versions and wants to try the remove space ant trick:
I found this to work better:
(\s+ removes 1 or more whitespace characters, so you're not inserting a space between two tags which don't currently have a space between them).
Also, I actually had to use &lt; and &gt; rather than < and > in my match / replace strings above.
Posted by JG on April 19, 2012 at 03:28 PM MDT #
Occasionally, I use JSP's to generate output where whitespace is meaningful. The extra space caused by the formatting (line breaks and indentation) breaks whatever format I'm generating output for with my JSP.
A clever way to get nice indenting in the JSP while controlling the output spacing is judicious use of <pre><%-- --%></pre> So rather than:
You write:
While not beautiful, it beats putting everything on one line and gives you 100% control over which spaces appear in the output and which don't.
Posted by markofca on July 24, 2014 at 04:32 AM MDT #