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 31 Comments
Search This Site
Recent Entries
- EhCache Project Busy this Summer
- Spontaneous Stuff Weekend
- Awesome Birthday Present: A Kegerator
- Maven Plugin for Running Integration Tests against Multiple Containers
- Presenting Web Frameworks of the Future Tomorrow in Denver
- My OSCON Aftermath
- OSCON 2008 Wrapup
- [OSCON 2008] Even Faster Web Sites by Steve Souders
- [OSCON 2008] CSS for High Performance JavaScript UI by Gavin Doughtie
- [OSCON 2008] The State of Lightning Talks
Posted by Jason Barker on March 23, 2005 at 11:27 PM MST #
Posted by Kris on March 24, 2005 at 12:08 AM MST #
Posted by Kris on March 24, 2005 at 12:09 AM MST #
Posted by Larry Williams on March 24, 2005 at 12:59 AM MST #
<!-- 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 01: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 01:12 PM MST #
<c:set var="dummy"> <%@ page errorPage="/Error.jsp" %> </c:set>Posted by Michael Slattery on March 25, 2005 at 07:12 AM MST #
Posted by Charles Havener on March 25, 2005 at 02:09 PM MST #
Posted by funkman on March 26, 2005 at 06: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 06: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 07: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 03:08 AM MST #
Posted by Mim on December 03, 2007 at 11:17 PM MST #
Posted by David Truong on January 30, 2008 at 12:52 PM MST #
Posted by jess on February 20, 2008 at 09: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 #