tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Luc Vanlerberghe <...@bvdep.com>
Subject Re: Whitespace
Date Fri, 02 Feb 2001 17:37:23 GMT
The whitespace is not generated by tomcat, but is present in your
source-jsp page between the jsp tags.
If you have a page that starts like:
<%@ page contentType="text/html"%>
<%@ page session="true"%>
<%@ page import="java.util.*,org.vanlerberghe.luc.spaans.*"%>
<jsp:useBean id="topicHome" type="TopicHome" scope="application"/>
<html>

then it may not be obvious, but there are 4 (four!) linefeeds between
the jsp tags and your first <html> tag.  The way I solve this, without
giving up the readability of my page too much is by putting the closing
bracket on the start of the next line like so:
<%@ page contentType="text/html"
%><%@ page session="true"
%><%@ page import="java.util.*,org.vanlerberghe.luc.spaans.*"
%><jsp:useBean id="topicHome" type="TopicHome" scope="application"
/><html>

This way "<html>" will be the very first characters written to output.

I do similar things with <% ... %> too, like:

<%
    for (int i=0;i<nTopic;i++) {
        Topic topic=topicHome.getTopic(i);
        
        String selected=(topic.selected) ? " selected" : "";
%><option value="<%=topic.id%>"<%=selected%>><%=topic.description%>
<%
    }
%>    

(Note the single linefeed at the end of the line starting with
"%><option")

This will generate a nice looking list in the html source like:
<option value="id0">description0
<option value="id1" selected>description1
<option value="id2">description2


I believe this trick is especially inportant to get the '<?xml
version="1.0"?>' at the start of the output, because XML requires it to
be at offset 0 (I think).

Luc Vanlerberghe

P.s.: By the way: there is no way to let tomcat do this automatically. 
It is impossible for it to know which whitespace is needed and which
not.  In html, whitespace sometimes *does* make a difference (e.g. in a
<pre> block)

Cato Førrisdahl wrote:
> 
> Hi,
> I have some jsp-pages which, when run, generates a lot of
> whitespace in the resulting page. It typically looks something like:
> ---Start---
> 
>   <?xml version="1.0"?>
> ...
> --Stop--
> Is there a way to prevent tomcat from making all this whitespace and make
> output like:
> --Start--
>   <?xml version="1.0"?>
> --Stop--
> 
> ?
> 
> Cato Førrisdahl
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, email: tomcat-user-help@jakarta.apache.org


Mime
View raw message