XHTML Validation Filter

Add "?vf-src=true" to the end of a URL that is mapped to the filter and you should see your source displayed with ValidationFilter.

How can I disable ValidationFilter in production?

Ensure your production url's are not in "enableOnHosts" in your web.xml

    <filter>
        <filter-name>ValidationFilter</filter-name>
        <filter-class>org.tuckey.web.filters.validation.ValidationFilter</filter-class>
        <init-param>
            <param-name>enableOnHosts</param-name>
            <param-value>localhost, 127.0.0.1, dev.foobar.com, *.test.foobar.com </param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>ValidationFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>

If you are using Tomcat and you are paranoid then to your [tomcat-directory]/conf/web.xml add:

    <context-param>
    <param-name>enableValidationFilter</param-name>
    <param-value>false</param-value>
    </context-param>

Under the <web-app> element near the top of the file on your production machines this will ensure that ValidationFilter never does anything.

Can I check all files even if they have no XHTML doctype specified?

If you want ValidationFilter to be aggressive and check everything is valid not just HTML pages with the XHTML DTD specified then add this init param (it's true by default).

    <init-param>
        <param-name>checkForDtdDefinition</param-name>
        <param-value>false</param-value>
    </init-param>

Can the XHTML errors be suppressed sometimes?

Set the init parameter "displayOriginal" to true and the error message will be displayed optionally.

    <init-param>
        <param-name>displayOriginal</param-name>
        <param-value>true</param-value>
    </init-param>

Can I use a different XML parser than the system default?

Yes, you can specify a custom XML parser (if you want to do this for your whole web application then specify it in the normal way in a system property with the key javax.xml.parsers.DocumentBuilderFactory), otherwise add the init-param below. Note, is this is not specified ValidationFilter will use the default XML parser.

    <init-param>
        <param-name>xmlParser</param-name>
        <param-value>org.apache.xerces.jaxp.DocumentBuilderFactoryImpl</param-value>
    </init-param>

Why doesn't ValidationFilter support HTMLx.x?

Because validating plain old HTML is rather difficult. The best thing to do is use W3C's validator.

Is ValidationFilter fast.

Yes pretty fast. But it is not recommended to have it enabled in poduction as it won't really help people using your site. It is a developers tool.

Why should I use XHTML and not plain old HTML?

Basically because XHTML is HTML based on XML. It means any old XML parser can do things with your content easily. Search engines (robots), translators, blind readers have a much easier time reading valid XHTML than old dirty HTML. XHTML is only good... when valid, otherwise it's useless. That is where ValidationFilter comes in. It will validate all your outgoing XHTML as you develop and show you errors so that you can fix them during development.

Better said at wdvl.com's Why use XHTML and Introduction to XHTML or w3schools.com's Learn XHTML or zvon.org's Excellent XHTML reference.