Press "Enter" to skip to content

JSP Documents and valid HTML

I have never been a fan of JavaServer Pages (JSP) but I have used them on a few projects. When JSP Documents (JSPs with XML syntax) were added in version 1.2 I thought if you had to do JSP then JSP Documents were the way to go. I prefer the uniform XML syntax to all the crazy syntax in JSP Pages.

Recently I’ve been focused on producing valid HTML. Perhaps too focused. A while back I bought into the XHTML is the wave of the future so we should all start using XHTML now hype. Now I see that serving good old HTML 4.01 is the best choice. See Sending XHTML as text/html Considered Harmful for why. My old habit of using XML style empty elements such as <br /> was getting me into trouble with HTML validators. The HTML Validator Firefox extension (using Tidy) will flag the slash in red even though it doesn’t give a written error. The W3C validator gives an error if the /> results in character data where it isn’t allowed. This can happen on the empty meta tag.

After reading about empty elements and what <br /> really means in HTML I wondered how is it even possible for a JSP Document to generate valid HTML.

Because JSP Documents must be valid XML an empty tag like meta must end with /> (or </meta> which doesn’t help) and this results in a validation error because character data is not allowed within the head tag.

This really surprised me. I looked and looked for a way to tell the JSP Document to output valid HTML but couldn’t find one. If you know of a way please tell me. The jsp:output action gives you control over the DOCTYPE and XML declaration. I was expecting it to also let you specify that the output method is HTML just like XSLT does. But it doesn’t.

Unless I’m missing something obvious JSP Documents should come with a warning – “Warning JSP Documents don’t output valid HTML by default”. I wonder why the oversight. Did the JSP designers buy into the XHTML hype as well?

There are two ways I know of to work around this issue. One is to use a CDATA section. For example:

    <![CDATA[<meta http-equiv="Content-Type" content="text/html">]]>

The other is to use a tag library such as Struts HTML that knows how to output either HTML or XHTML. But this doesn’t help with the meta tag.

Even with this minor snag I still prefer JSP Documents to JSP pages but that isn’t saying much.


  1. Zachary
    Zachary Tuesday, September 4, 2007

    uncle john, we like your lego video. i asked dad to let me see it again tonight. i miss alexandria and issabella. dad says he’s glad you’re writing again.

  2. John Snyders
    John Snyders Monday, August 27, 2007

    Don, you are right it is difficult to keep the blog pages valid. At some point I should change my blog template to be HTML 4.01 strict rather than XHTML strict.

  3. Don Foley
    Don Foley Monday, August 27, 2007

    Hey John,

    I am always trying to keep my site xhtml 1.0 transitional compliant. But it gets tricky when I want to show a video. is invalid. Some times I can get it to work using alone but sometimes I have no choice but to use embed.

    by the way…

    Tag, you’re it. Please read my post:
    and then feel free to post 8 facts concerning yourself!

Comments are closed.