JSP FAQ
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Introduction
JSP Compilation
Reference
Tutorials
Articles
FAQ
JSP Templates
JSP
Quercus

Why I am getting extra whitespace in the output of my JSP?

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


JSP Templates
JSP
Quercus
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.