trim whitespace from jsps

Discussion in 'Java' started by Peter Horlock, Jul 24, 2008.

  1. Hi, I want to remove all unnessary white spaces from my jsps (to
    reduce file size and for search engine optimization...)

    I found the Tomcat param (web.xml):
    <init-param>
    <param-name>trimSpaces</param-name>
    <param-value>true</param-value>
    </init-param>

    Which does a nice job - but for some reason it even removes spaces it
    should NOT!!
    The problem (and a few work arrounds a didn't like too much) is
    described in more detail here:
    http://raibledesigns.com/rd/entry/trim_spaces_in_your_jsp1

    Any other ideas of how to fix this?
    &nbsp; and so on doesn't really get you a clean html code...,
    <c:eek:ut value=""/> and so on will clutter your jsp code...

    Maybe this problem is fixed with Tomcat 6.x?

    Any other solutions of how to get this done?

    Thanks in advance,

    Peter
     
    Peter Horlock, Jul 24, 2008
    #1
    1. Advertising

  2. Peter Horlock

    Dave Miller Guest

    Peter Horlock wrote:
    > Hi, I want to remove all unnessary white spaces from my jsps (to
    > reduce file size and for search engine optimization...)
    >
    > I found the Tomcat param (web.xml):
    > <init-param>
    > <param-name>trimSpaces</param-name>
    > <param-value>true</param-value>
    > </init-param>
    >
    > Which does a nice job - but for some reason it even removes spaces it
    > should NOT!!
    > The problem (and a few work arrounds a didn't like too much) is
    > described in more detail here:
    > http://raibledesigns.com/rd/entry/trim_spaces_in_your_jsp1
    >
    > Any other ideas of how to fix this?
    > &nbsp; and so on doesn't really get you a clean html code...,
    > <c:eek:ut value=""/> and so on will clutter your jsp code...
    >
    > Maybe this problem is fixed with Tomcat 6.x?
    >
    > Any other solutions of how to get this done?
    >
    > Thanks in advance,
    >
    > Peter

    I don't have an answer for you, rather two questions. How much file
    space do you save (total with and without) and (OT) how does whitespace
    reduction help with SEO?

    --
    Dave Miller
    Java Web Hosting at:
    http://www.cheap-jsp-hosting.com/
     
    Dave Miller, Jul 24, 2008
    #2
    1. Advertising

  3. Hi Dave,

    I haven't measured file space reduction yet - however I am sure you
    won't save that much, and you will
    save more if you turn on gzip compression (but saving some space doesn't
    hurt either, the smaller your pages are, the faster clients will receive
    them...)
    Concerning SEO, we are creating webpages based on data in our database
    - the entire content is database based - the title tag, description tag,
    well, really, all content.
    Anyway, so the title tag is generated in a loop,
    and as it is programmed using a JSTL for loop, the title is divided into
    several lines. For some weird reason the G. Bot is to stupid or to lazy
    to parse the entire tile tag => We need to find a way to write the
    entire title (and the other meta tags too) in one single line.

    Peter
     
    Peter Horlock, Jul 24, 2008
    #3
  4. Peter Horlock

    Daniel Pitts Guest

    Peter Horlock wrote:
    > Hi Dave,
    >
    > I haven't measured file space reduction yet - however I am sure you
    > won't save that much, and you will
    > save more if you turn on gzip compression (but saving some space doesn't
    > hurt either, the smaller your pages are, the faster clients will receive
    > them...)
    > Concerning SEO, we are creating webpages based on data in our database
    > - the entire content is database based - the title tag, description tag,
    > well, really, all content.
    > Anyway, so the title tag is generated in a loop,
    > and as it is programmed using a JSTL for loop, the title is divided into
    > several lines. For some weird reason the G. Bot is to stupid or to lazy
    > to parse the entire tile tag => We need to find a way to write the
    > entire title (and the other meta tags too) in one single line.
    >
    > Peter
    >

    The problem is that either Space Is Important, or Space Is Not
    Important, you can't have it both ways.

    In my opinion, this is a flaw with most template based languages (JSP,
    PHP, Freemarker, Velocity, etc...)

    One approach, which is still kind of ugly, but it works:

    <%----%><c:forEach ...><%--
    --%>Some thing that is repeated<%--
    --%></c:forEach>

    This allows you to keep some basic formatting while reducing whitespace.

    The other alternative is to avoid using templates, and build up a DOM
    Document programamtically, and then serialize the DOM as a text-stream.
    This has quite a few disadvantages, but it is the "cleanest" output
    (it ensures proper encoding and markup syntax).

    Hope this helps,
    Daniel.
    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jul 25, 2008
    #4
  5. Daniel Pitts schrieb:
    >>

    > The problem is that either Space Is Important, or Space Is Not
    > Important, you can't have it both ways.

    Well I never said I would not need ANY space (everyone needs SOME space!
    ;-)

    The problem is that the words sometimes willbeputtogetherlikethisline...

    >
    > One approach, which is still kind of ugly, but it works:
    >
    > <%----%><c:forEach ...><%--
    > --%>Some thing that is repeated<%--
    > --%></c:forEach>


    Do you want to say "fill the remaining space with jsp comments"?
    Not a bad idea. Will try this on Monday.

    Thanks,

    Peter
     
    Peter Horlock, Jul 26, 2008
    #5
  6. Hi Dave, in the meantime, I measured the file space savings:
    ~2,4 % for a big html page (433 lines vs. 258 lines of html).
    When I manually compressed both these pages, it still saved 1,9 %.

    So in conclusion, it's not that much, yet it's still something,
    whether you are sending compressed pages or not.
    For a big website like ours, this will save a few GBs of traffic, and
    every page will load just a tiny bit faster for every single client -
    all this by just adding 4 extra lines to your tomcat's web.xml.

    However, turning on html compression will reduce file size by 70%!

    Peter
     
    Peter Horlock, Jul 28, 2008
    #6
  7. Peter Horlock

    Dave Miller Guest

    Peter Horlock wrote:
    > Hi Dave, in the meantime, I measured the file space savings:
    > ~2,4 % for a big html page (433 lines vs. 258 lines of html).
    > When I manually compressed both these pages, it still saved 1,9 %.
    >
    > So in conclusion, it's not that much, yet it's still something,
    > whether you are sending compressed pages or not.
    > For a big website like ours, this will save a few GBs of traffic, and
    > every page will load just a tiny bit faster for every single client -
    > all this by just adding 4 extra lines to your tomcat's web.xml.
    >
    > However, turning on html compression will reduce file size by 70%!
    >
    > Peter

    Thanks, Peter.

    --
    Dave Miller
    Java Web Hosting at:
    http://www.cheap-jsp-hosting.com/
     
    Dave Miller, Jul 28, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FAQ server
    Replies:
    0
    Views:
    177
    FAQ server
    Aug 29, 2006
  2. FAQ server
    Replies:
    0
    Views:
    164
    FAQ server
    Oct 26, 2006
  3. FAQ server
    Replies:
    6
    Views:
    256
    Jonas Raoni
    Dec 25, 2006
  4. FAQ server
    Replies:
    26
    Views:
    355
    Dr J R Stockton
    Feb 26, 2007
  5. FAQ server
    Replies:
    2
    Views:
    162
    -Lost
    Apr 24, 2007
Loading...

Share This Page