9

This was a problem when I tried to switch to XML-based JSP over 15 years ago, and it looks like it's still a problem.

Using Tomcat 9, if I have a simple JSP page, the output is nicely formatted.

<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en-US">
<head>
  <meta charset="UTF-8"/>
  <title>Example</title>
</head>
<body>
  <p>Example</p>
</body>
</html>

The output looks pretty much the same as the source. But if I use a JSP document (JSPX):

<?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; charset=UTF-8" />
<jsp:text>&lt;!DOCTYPE html&gt;</jsp:text>
<html lang="en-US">
<head>
  <meta charset="UTF-8"/>
  <title>Example</title>
</head>
<body>
  <p>Example</p>
</body>
</html>
</jsp:root>

Then the output just becomes one long line:

<!DOCTYPE html><html lang="en-US"><head><meta charset="UTF-8"/><title>Example</title></head><body><p>Example</p></body></html>

Yes, I understand the reasons behind this: parsing to the XML document model, manipulating the tree, reserializing, etc. But as a practical matter: it's ugly, I don't like it, and probably nobody likes it. What's the easiest, most practical way to get some reasonable line breaks and indention in the output?

The fact that the browser doesn't care is beside the point. As developers we have to work with this. This is ugly and gets in the way. (Yes, I know probably nobody uses JSPX or even JSP, but I thought I'd ask anyway.)

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • Wow, nobody has a clue on how to do this? I'd put up another bounty, but now it would be up to 200 and I feel like would just be wasting my reputation. – Garret Wilson Nov 27 '18 at 12:57

1 Answers1

0

Copied from Strip whitespace from jsp output There is a trimWhiteSpaces functionality that may be your problem:

In your JSP there could be:

<%@ page trimDirectiveWhitespaces="true" %>

which you should set to false and in the jsp-config section your web.xml:

<jsp-config>
  <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <trim-directive-whitespaces>false</trim-directive-whitespaces>
  </jsp-property-group>
</jsp-config>

You can take a look here for further configurations you can tune in you Tomcat 9 server.

rakwaht
  • 3,666
  • 3
  • 28
  • 45
  • First of all the whole point of the question was to use JSPX, so I don't know why `<%@ page trimDirectiveWhitespaces="true" %>` was mentioned, as this is the non-XML form of the directive. Nevertheless I tried `` and it made not difference on the output of my JSPX. Plus if you read https://stackoverflow.com/a/43188063/421049 and other documentation such as https://docs.oracle.com/cd/E19316-01/819-3669/bnajm/index.html you'll see that this directive has nothing to do with the question. – Garret Wilson Oct 27 '18 at 21:47
  • @GarretWilson my bad, I didn't get your question then. I will let the answer be since it seems that is was useful to somebody else – rakwaht Oct 29 '18 at 08:52