How can you strip HTML comments out of a page generated with JSP/JSTL?

I know some people use ANT to accomplish this, but I don't want to remove HTML comments from the actual jsp, I just want a way to strip them from output unless a users adds a parameter to the url to show the comments for debugging.


Here is HTMLCompressor lib that can be called either from code or as JSP tag to strip comments and more.

You can use JtidyFilter with hide-comments set to true.

    <param-value>hide-comments: true</param-value>

To make it more configureable, make use of JTidy in your own custom filter.

Simply put, you need a filter in front of the JSP. Something to post-process the results.

The filter looks at the HTTP request for the appropriate parameter, and decides from there whether or not to filter the outgoing HTML results to the user.

As far as performing the filtering, you may be able to use a simple REGEX for that. The downsides of a REGEX is simply that you will need to duplicate the entire outgoing result in memory before sending it out to the user. This consumes not just memory, but time, depending on how your results are rendered. But, in theory, a REGEX can work.

Better would be a streaming HTML lexer to read the contents bit by bit, and filter out the comments that way. I can't suggest any to use, I wrote my own.

But, a filter is what you want.

Need Your Help

CreateService or StartService hangs on windows 7 x64

windows winapi service windows-7 windows-7-x64

CreateService or StartService hangs on windows 7 x64 when i trying to use it to register new service from service.

Disable jQueryUI datepicker dates based on Select value change

javascript php jquery jquery-ui datepicker

When a value is selected from a SELECT list, an existing empty array var disableddates = []; gets populated with a list of dates in a '2016-03-21' format.