1

I want to output the following DOCTYPE specifier with jspx:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

This seems to be impossible. What I tried is:

<jsp:output doctype-root-element="HTML" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />

...which results in:

org.apache.jasper.JasperException: /WEB-INF/layouts/fittopage.jspx(3,100) &lt;jsp:output&gt;: 'doctype-root-element' and 'doctype-system' attributes must appear together

(I'm using Tomcat 6.0). I don't want to include a SYSTEM identifier containing the DTD's URI (in this case doctype-system="http://www.w3.org/TR/html4/loose.dtd") because that reproducibly causes browsers (Firefox and Chrome) to render the page differently (or not at all).

Olaf Klischat
  • 727
  • 1
  • 7
  • 14
  • The site was apparently originally developed/styled in MSIE with a wrong doctype? I strongly, strongly recommend to go for a real strict/standards mode doctype like ` ` instead of a quirks mode doctype (so that it looks at least the same in all browsers) and fix the CSS issues accordingly. See also http://hsivonen.iki.fi/doctype/ – BalusC Oct 05 '11 at 01:28
  • Well, not IE (we're not even supporting that for now), but it is a site that has grown over time, uses Javascript (dojo) heavily, and can't be changed overnight. And the fact remains that the site renders correctly with said sole PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" declaration and does not render at all with a strict declaration, or with a system dtd added to the declaration. And it is apparently legal to have a DOCTYPE declaration that contains only a PUBLIC identifier, so I'm wondering why jspx doesn't support it (or does it?). – Olaf Klischat Oct 05 '11 at 02:10
  • 2
    I'm no JSPX guy, but try specifying an empty value `doctype-system=""`. – BalusC Oct 05 '11 at 02:13
  • @BalusC's suggestion worked for me on Tomcat 9. – Garret Wilson Oct 14 '18 at 15:15
  • At first I thought that @BalusC's suggestion worked for me on Tomcat 9, but it actually produced `SYSTEM ""`, which I don't want. :( – Garret Wilson Oct 14 '18 at 15:20

1 Answers1

3

Re: Garret Wilson's question: "So how do I output <!DOCTYPE html> for HTML 5 with the JSP document (JSPX) syntax? Is it even possible?"

This can be done with jsp:text. (It cannot be done with jsp:output).

The current version of JSP specification is JavaServer Pages Specification Version 2.3. (Implemented in current Java EE 8 (Tomcat 9.x) as well as previous Java EE 7 (Tomcat 8.x)). Quoting from chapter JSP.5.6 "<jsp:output\>":

The doctype-root-element, doctype-system and doctype-public properties allow the page author to specify that a DOCTYPE be automatically generated in the XML prolog of the output. Without these properties, the DOCTYPE would need to be output manually via a <jsp:text> element before the root element of the JSP document, which is inconvenient.

A DOCTYPE must be automatically output if and only if the doctype-system element appears in the translation unit as part of a <jsp:output> action. The doctype-root-element must appear and must only appear if the doctype-system property appears, or a translation error must occur. The doctype-public property is optional, but must not appear unless the doctype-system property appears, or a translation error must occur.

Until somebody asks the specification committee for more flexibility here, and a new version of specification comes out, this is all that we have.

An example using jsp:text:

<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3">
   <jsp:directive.page contentType="text/html" />
   <jsp:text>&lt;!DOCTYPE html&gt;</jsp:text>
   <html lang="en">
     <head>
       <meta charset="${pageContext.response.characterEncoding}"/>
       <title>Hello world</title>
     </head>
     <body>
       <h1>Hello world!</h1>
     </body>
   </html>
</jsp:root>
Konstantin Kolinko
  • 3,854
  • 1
  • 13
  • 21
  • That was a quick response and a complete answer! Looks good. Just what I was looking for. Thanks for clearing that up. It looks like the JSP XML format unfortunately isn't much more helpful than when I last used it more than 15 years ago. – Garret Wilson Oct 14 '18 at 17:04
  • I found it interesting that if I leave off the `` and put the namespaces on the `` (with the directives underneath that), the output spits out a `` which I don't want for a polyglot document. So I guess I'm stuck with using ``, which I guess is OK. – Garret Wilson Oct 15 '18 at 15:00
  • Since you provided such a great answer here, you might consider looking at my other question as well: https://stackoverflow.com/q/52819348/421049 – Garret Wilson Nov 27 '18 at 14:30