Filter Overview

The Java Servlet specification includes component type, called a filter. A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses. Filters typically do not themselves create responses, but instead provide universal functions that can be "attached" to any type of servlet or JSP page. Filters can be used to transform the response from a servlet or a JSP page.

For more help with Filters in general, please see:

The Essentials of Filters @java.sun.com

Filter code with Servlet 2.3 model @javaworld.com

What JTidyFilter does?

The JTidyFilter dynamically intercepts responses generated by your JSPs or servlets and changes or validate HTML using JTidy. It does the same job as <jtidy:tidy>..</jtidy:tidy> JSP Tags. When using filter you don't need to modify your applictions JSP. Also it will work when HTML is denerated by non JSP servlets or by static HTML files.

JTidyFilter configuration

A Filter is a component that is invoked when a client requests a resource that the Filter is mapped to, such as a URL pattern or a Servlet name. You can apply a filter to certain URL patterns or servlet names using the <filter-mapping> tag in web.xml.

<filter-mapping>
    <filter-name>JTidyFilter</filter-name>
    <url-pattern>/servlet/Foo</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>JTidyFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
</filter-mapping>
                    

<url-pattern> A pattern matching the url: /foo/*, *.foo or /foo the same as <url-pattern> in <servlet-mapping> A servlet container interprets the url-pattern according to the following rules:

  • /foo/* A string beginning with a / and ending with the /* characters is used for determining a servlet path mapping.
  • *.foo A string beginning with a *. prefix is used to map the request to a Filter that handles the extension specified in the string.
  • /foo All other strings are used as exact matches only.

FYI: Macromedia example, to map a filter to all JSPs in a web application's context <url-pattern>/*.jsp</url-pattern> Is not valid example and does not work in JRun 4 and Tomcat 5

JTidyFilter init-param

All <init-param> are optional for JTidyFilter.

Example of Filter configuration in /WEB-INF/web.xml file.

<web-app>
    ...
    <filter>
        <filter-name>JTidyFilter</filter-name>
        <filter-class>org.w3c.tidy.servlet.filter.JTidyFilter</filter-class>
        <init-param>
            <param-name>properties.filename</param-name>
            <param-value>MyJTidyServlet.properties</param-value>
        </init-param>
        <init-param>
            <param-name>tee</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>config</param-name>
            <param-value>indent: auto; indent-spaces: 2</param-value>
        </init-param>
        <init-param>
            <param-name>doubleValidation</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>validateOnly</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>doubleValidation</param-name>
            <param-value>false</param-value>
        </init-param>
    </filter>
    ...
</web-app>

                    
<init-param>DefaultValid ValuesDescription
teefalsetrue, falseDo not buffer the output, Preform validation only. This may send the responce back to browser while we are still parsing the HTML. May solve problem for some applications that are flushing the OutputStream.
validateOnlyfalsetrue, falsevalidateOnly only do not change output.
doubleValidationfalsetrue, falsePerforms validation of html processed by <jtidy:tidy> jsp tag By default this is not done. Only Usefull for testing JTidy. This will create second requestID to store the data.
configString, name: value;JTidy Parser configutation string. Examples of config string: indent: auto; indent-spaces: 2
properties.filenameStringCustom properties file path. See: Configuration