17

This would be an HTML minifier that skips everything between <% and %>.

Actually, an Open Source HTML minifier would be a good starting place, especially if it already had code to preserve the contents certain blocks like <textarea. It's code might be able to be made to preserve <%%> blocks also.

I am aware that HTML minifiers are less common because that changes more often than JS/CSS and is often dynamically generated, but if the JSP compiler could be made to minify before making its compiled cache copy, it would result in minified HTML.

Also, an ASP minifier would probably be very close to the same thing. And I don't care about custom tags that have meaning to the server. The only stuff that matters to the server (for my company) is in the <%%> blocks.

700 Software
  • 85,281
  • 83
  • 234
  • 341
  • 3
    If your purpose is to save bandwidth, all modern HTTP servers and all browsers has support for transparent gzipping, which is usually easier and more effective than minification. If your purpose is to obfuscate the HTML code, then don't bother, Firebug and/or tidy will fix them in no time. – Lie Ryan Mar 30 '11 at 15:49

4 Answers4

12

This question is a bit outdated but an answer with a resource still hasn't made it's way to the posting.

HtmlCompressor makes this very thing possible and quite simply.

You can use it via Java API:

String html = getHtml(); //your external method to get html from memory, file, url etc.
HtmlCompressor compressor = new HtmlCompressor();
String compressedHtml = compressor.compress(html);

Or you can use it via Taglib:

Download .jar file of the current release and put it into your lib/ directory
Add the following taglib directive to your JSP pages:

<%@ taglib uri="http://htmlcompressor.googlecode.com/taglib/compressor" prefix="compress" %>

Please note that JSP 2.0 or above is required.

In JSP:

<compress:html removeIntertagSpaces="true">
    <!DOCTYPE html>
    ...
    </html>
</compress:html>

Cheers

  • Careful! I tried it with one of my projects on Google App Engine. Processing time increased from ∼20 ms to ∼100 ms. With compresCss="true" it was even ∼200 ms. – Marcus Sep 22 '16 at 07:53
5

JSP is transformed to Java code and subsequntly compiled to bytecode. Minifying JSP has no purpose then.

You can process output generated by JSP page by writing custom filter. I have written filter to trim empty lines and unnecessary whitespace from JSP output, unfortunately it's not public. But if you google around, I'm sure you can find servlet filters to remove unneeded stuff from generated HTML.

Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
  • 5
    If you minify the JSP before it is turned in to Java code, then the final output will be minified - Therefore, Minifying JSP has a purpose. I do not want a runtime filter because it would have to run every time the JSP's Servlet runs and it would even strip white space I want that I put in like this `<%="\n \n"%>` – 700 Software Apr 11 '11 at 12:29
  • @George, the problem is that invisible java code from jsp page produces much more garbage whitespace than your own html. You can't get rid of it by minifying JSP before it is translated. It's possible to get rid of it by turning on 'trimSpaces' option as mentioned in other comment, but it use to introduce bugs into html/js code :( I believe filter is the best option. – Peter Štibraný Apr 11 '11 at 15:15
  • In my case, Minified JSPs would result in minified HTML. I am sure there are situations where this would not be the case. Mine is not one of them. – 700 Software Apr 11 '11 at 21:43
  • @George, maybe you can rewrite JSP code to use XML syntax and use some standard HTML minifier then? – Peter Štibraný Apr 12 '11 at 11:05
2

Have a look at the Trim Filter (http://www.servletsuite.com/servlets/trimflt.htm), which you can simply map in your web.xml. It will help you to remove whitespace, and can also strip off comments.

From my experience, whitespace occurs a lot in JSPs if you use tags that themselves don't have any output, such a the JSTL C control tags (c:if, c:choose, ...), and then this comes in very handy.

Rudolf Mayer
  • 377
  • 2
  • 7
  • 3
    OP wants to trim it during compiletime, not during runtime. As to the whitespace left by JSP tags/scriptlets, for that `trimSpaces` attribute of the JspServlet can be used, but the OP likely already knows that :) – BalusC Mar 31 '11 at 16:55
  • It does not trim whitespace between HTML tags, so that does not answer your question. – BalusC Mar 31 '11 at 18:10
  • trimSpaces has bugs, at least in tomcat 5.5 :-( – Peter Štibraný Apr 10 '11 at 09:15
  • Trim Filter is not free for commercial use; does anyone know of an open-source non-GPL equivalent? – Richard Barnett Mar 13 '12 at 23:59
0

As you are already aware that HTML minification is less common and it also results in errors sometime than getting any benefit out of it. HTML is also dynamically generated content.

On the other hand, there are many better ways to speed up the application front end.

WebUtilities is a small java library to help speed up J2EE webapp front-end. Below is the link.

http://code.google.com/p/webutilities/

With new 0.0.4 version it does many optimization and results in significant performance boost. Please have a look in case you find it useful.

Rajendra
  • 29
  • 1