Wednesday March 23, 2005
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.
Posted in Java
at Mar 23 2005, 10:24:58 PM MST
36 Comments
Search This Site
Recent Entries
- Twitter's Open Source Summit: Bootstrap 2.0 Edition
- Refreshing AppFuse's UI with Twitter Bootstrap
- 2011 - A Year in Review
- Upgrading AppFuse to Spring Security 3.1 and Spring 3.1
- What have I been working on at Taleo?
- Our Engaging Trip to Paris and Antwerp
- My HTML5 with Play Scala, CoffeeScript and Jade Presentation from Devoxx 2011
- Deploying Java and Play Framework Apps to the Cloud with James Ward
- PhoneGap for Hybrid App Development
- Play 2.0, A web framework for a new era
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 12: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 01: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 01:09 AM MST #
Posted by Larry Williams on March 24, 2005 at 01:59 AM MST #
if you want to get rid of really ALL whitespaces (not just between directives) try this in your build file:
<!-- Strip whitespace --> <replaceregexp match="&gt;\s*&lt;" replace="&gt;&lt;" flags="g" byline="false"> <fileset dir="${build.home}" includes="**/*.html,**/*.htm,**/*.jsp,**/*.tag"/> </replaceregexp>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 02: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 02:12 PM MST #
<c:set var="dummy"> <%@ page errorPage="/Error.jsp" %> </c:set>Posted by Michael Slattery on March 25, 2005 at 08:12 AM MST #
Posted by Charles Havener on March 25, 2005 at 03:09 PM MST #
Posted by funkman on March 26, 2005 at 07:11 PM MST #
Posted by Michael Musson on April 05, 2005 at 11:01 AM 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 12:50 PM MDT #
Posted by Noruas on March 14, 2006 at 07:26 AM MST #
<!-- Strip whitespace --> <replaceregexp match=">\s*<" replace="> <" flags="g" byline="false"> <fileset dir="${build.home}" includes="**/*.html,**/*.htm,**/*.jsp,**/*.tag"/> </replaceregexp>I'm using ant 1.6.1. The task above also just condenses between tags from many spaces to one space, rather than remove all of them (in case your tags output something that needs a space between them). I have found that you don't save as much doing the stripping at build time as you do at runtime.Posted by Dan Moore on June 26, 2006 at 02:25 PM MDT #
Posted by Patrick O'Sullivan on August 23, 2006 at 06:20 AM MDT #
Posted by Jason Andersen on August 25, 2006 at 11:24 PM MDT #
Posted by todd hodes on September 12, 2006 at 08:07 PM MDT #
Posted by Viz on November 06, 2006 at 08:10 AM MST #
Posted by zzAA on May 23, 2007 at 01:42 AM MDT #
Posted by Nataraj on May 31, 2007 at 12: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 -
<filter> <filter-name>trimFilter</filter-name> <filter-class>com.cj.trim.trimFilter</filter-class> <init-param> <param-name>removeComments</param-name> <param-value>true</param-value> </init-param> </filter>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 03:43 AM MDT #
Posted by Ashish Nair on July 03, 2007 at 03:47 AM MDT #
Posted by Sanath Kodikara on July 11, 2007 at 12:41 AM MDT #
Posted by Matt Raible on July 11, 2007 at 12:45 AM MDT #
Posted by Raible Designs on August 01, 2007 at 05:02 PM MDT #
Posted by Balakumar on October 25, 2007 at 04: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 04:08 AM MST #
Posted by Mim on December 04, 2007 at 12:17 AM MST #
Posted by David Truong on January 30, 2008 at 01:52 PM MST #
Posted by jess on February 20, 2008 at 10:23 AM MST #
<!-- Strip whitespace --> <replaceregexp match=">\s*<" replace="> <" flags="g" byline="false"> <fileset dir="${build.home}" includes="**/*.html,**/*.htm,**/*.jsp,**/*.tag"/> </replaceregexp>To remove line breaks and comments in JSP's?Posted by Sam Allen on April 04, 2008 at 04:55 AM MDT #
Posted by leoj on May 12, 2008 at 03: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 03:48 AM MDT #
http://java.sun.com/developer/technicalArticles/J2EE/jsp_21/
Posted by Will on January 31, 2010 at 10:14 AM MST #
Posted by Will on January 31, 2010 at 10:15 AM 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 02:34 PM MDT #
Posted by Matt Raible on September 30, 2010 at 03:00 PM MDT #