views:

238

answers:

3

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.

+1  A: 

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

serg
+1  A: 

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.

Will Hartung
+1  A: 

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.

BalusC
ok but how do I use this with a querystringhttp://myurl.com?showcomments=true
codeninja
Read the last line of my answer.
BalusC