|
I'm getting a lot of whitespace in my JSP that I don't intend to be there.
Why is it appearing and how can I get rid of it?
The extra whitespace is coming from newlines, often at the end of
declaration lines at the beginning of the JSP. For example, the following
jsp:
<%@ page import='java.util.*' %>
<%@ page import='java.io.*' %>
Hello world
Has newlines in these locations:
<%@ page import='java.util.*' %>NL
<%@ page import='java.io.*' %>NL
Hello worldNL
The result contains the newlines, which may be surprising:
Hello world
One solution is to let the JSP tag extend across the newline:
<%@ page import='java.util.*'
%><%@ page import='java.io.*'
%>Hello world
Another solution is to use JSP comments to remove the newlines:
<%@ page import='java.util.*' %><%--
--%><%@ page import='java.io.*' %><%--
--%>Hello world
Another solution is to use the XML syntax of JSP. Parsing of XML causes
removal of extra whitespace.
<jsp:root>
<jsp:directive.page import="java.util.*"/>
<jsp:directive.page import="java.io.*"/>
<jsp:text>Hello world</jsp:text>
</jsp:root>
Resin also supports the use of the '\' character to eat whitespace (this is
a Resin specific feature):
<%@ page import='java.util.*' %>\
<%@ page import='java.io.*' %>\
Hello world
Copyright (c) 1998-2009 Caucho Technology, Inc. All rights reserved. caucho® ,
resin® and
quercus®
are registered trademarks of Caucho Technology, Inc.
|