qodeninja
qodeninja

Reputation: 11276

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.

Upvotes: 1

Views: 2099

Answers (3)

BalusC
BalusC

Reputation: 1109222

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

<init-param>
    <param-name>config</param-name>
    <param-value>hide-comments: true</param-value>
</init-param>

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

Upvotes: 2

Will Hartung
Will Hartung

Reputation: 118744

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.

Upvotes: 1

serg
serg

Reputation: 111325

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

Upvotes: 1

Related Questions