betweenGo

Trim White Space from JSP

by Frank Kim on Aug.22, 2008, under JSP, Page Development

Sometimes the resultant HTML from JSP files has too much white space.  This is because a lot of JSP logic will end up in only a few lines of actual HTML code and the rest is white space.

One efficient way to get rid of white space is to add this configuration to your web.xml on Tomcat/JBoss as described in this article, Trim Spaces in your JSP’s HTML.

/jboss/jboss-eap-4.2/jboss-as/server/atg/deploy/jboss-web.deployer/conf/web.xml

<servlet>
    <servlet-name>jsp</servlet-name>
    ...

    <init-param>
        <param-name>trimSpaces</param-name>
        <param-value>true</param-value>
    </init-param>

    ...
</servlet>


However this sometimes has undesired side-effects. For example this DSP:

<div class="foo">
  <dspel:valueof param="displayName" />
</div>


causes this undesired result:

<div class="foo"/>
  Frank Kim


The work around is to do this.

<dspel:getvalueof  var="displayName" param="displayName" />
<div  class="foo"><c:out  value="${displayName}"/></div>

The above problem might have been happening because we are running with a version of ATG that is for Servlet 2.3 but we are running with a version of JBoss that runs with Servlet 2.4.  (Update 06-09-2010: For ATG 9.1 with JBoss EAP 4.2 I don’t see this problem.)

There is another tip on removing white space in this JSP FAQ: Why I am getting extra whitespace in the output of my JSP?  In our code we had one file which included a bunch of tag libraries.  It originally looked like this.

<%/** Taglibs.jsp: include to pull in all taglibs */%>

<%@ taglib uri="/dspELTaglib" prefix="dspel" %>
<%@ taglib uri="/jstlCoreTaglib" prefix="c" %>
<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-tiles" prefix="tiles" %


When we changed it to this we saved about 5% in terms of page size in bytes.

<%@ taglib uri="/dspELTaglib" prefix="dspel"
%><%@ taglib uri="/jstlCoreTaglib" prefix="c"
%><%@ taglib uri="/struts-bean" prefix="bean"
%><%@ taglib uri="/struts-tiles" prefix="tiles" %>


You can use this tip with a DSP page and maintain indentation.

<dspel:page   ><dspel:droplet name="/betweengo/droplet/Foo"     ><dspel:oparam name="output"       >Name: <dspel:valueof param="name"     /></dspel:oparam   ></dspel:droplet
></dspel:page>


  • Share/Bookmark

Related posts:

  1. Size of collection in a JSP/DSP page
  2. Accessing RepositoryItems with JSTL
  3. Display formatted HTML text
  4. Comparison of DSP and DSPEL
  5. Droplet Name Case Sensitivity

:, ,

1 Trackback or Pingback for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!