I wrote a filter to strip these lines out.  My application returned a CSV document 
that looked strange in Excel with all of the blank lines caused by the various JSP 
constructs that aren't visible to the user.  In my case, the rendering of the 
whitespace in the document is important.  You're right that this is not usually a 
problem with HTML and browsers.

Here's the filter and a supporting class

// BlankLineFilter.java
package mypackage.filter;

import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

public class BlankLineFilter implements Filter
{
    public void init(FilterConfig _config) {}

    public void doFilter(ServletRequest _request,
                         ServletResponse _response,
                         FilterChain _chain)
        throws IOException, ServletException
    {
        PrintWriter out = _response.getWriter();

        CharResponseWrapper wrapper = new 
CharResponseWrapper((HttpServletResponse)_response);

        _chain.doFilter(_request, wrapper);

        StringReader sr = new StringReader(wrapper.toString());
        BufferedReader br = new BufferedReader(sr);
        String line = null;
        while( (line = br.readLine()) != null ) {
            if( line.trim().equals("") ==  false )
                out.println( line );
        }
        br.close();
    }

    public void destroy() {}
}
// end of BlankLineFilter.java

// CharResponseWrapper.java
package mypackage.filter;

import java.io.PrintWriter;
import java.io.CharArrayWriter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class CharResponseWrapper extends HttpServletResponseWrapper
{
    private CharArrayWriter output;

    public CharResponseWrapper(HttpServletResponse _response) {
        super(_response);
        output = new CharArrayWriter();
    }

    public String toString() {
        return output.toString();
    }

    // have filter caller fill up 'output'
    public PrintWriter getWriter() {
        return new PrintWriter(output);
    }
}
// end of CharResponseWrapper.java

To hook this up to your app, use the following web.xml file.

    <filter-name>BlankLineFilter</filter-name>
    <filter-class>mypackage.BlankLineFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>BlankLineFilter</filter-name>
    <url-pattern>/myjsp.jsp</url-pattern>
  </filter-mapping>




----- Original Message -----
From: Jerry Jalenak <[EMAIL PROTECTED]>
Date: Friday, February 20, 2004 4:56 pm
Subject: RE: Extra Lines in Compiled JSP

> Thanks to everyone and for their comments.
> 
> What I was seeing were several blank lines at the top of my 
> rendered page -
> lines that I could not account for in the html code.  After 
> scratching my
> head for a few days, it occurred to me that the number of blank 
> lines being
> rendered were the same number of lines as my <% taglib %>'s - in 
> this case
> seven lines.  Opening up the compiled JSP I found where it was 
> putting seven
> out.write() statements.  I know the browser should ignore 
> successive white
> space like this, but in my case it wasn't (this is an IE 6 
> browser).  
> 
> We're running Tomcat 5.0.18 and J2SDK 1.4.2.
> 
> Jerry Jalenak
> Development Manager, Web Publishing
> LabOne, Inc.
> 10101 Renner Blvd.
> Lenexa, KS  66219
> (913) 577-1496
> 
> [EMAIL PROTECTED]
> 
> 
> > -----Original Message-----
> > From: Piper, James D CECOM SEC EPS
> > [EMAIL PROTECTED]
> > Sent: Friday, February 20, 2004 3:48 PM
> > To: 'Adam Hardy'; Struts Users Mailing List
> > Subject: RE: Extra Lines in Compiled JSP
> > 
> > 
> > Why do you have this problem with extra lines in your html 
> > generated by the
> > <%@ %> section of your jsp and I do not - I use JBOSS and 
> > WebLogic both --
> > never saw this problem?
> > 
> > - Jim
> > 
> > 
> > -----Original Message-----
> > From: Adam Hardy [EMAIL PROTECTED]
> > Sent: Friday, February 20, 2004 3:42 PM
> > To: Struts Users Mailing List
> > Subject: Re: Extra Lines in Compiled JSP
> > 
> > 
> > Perhaps it was in the taglibs-user mailing list where I dreamt it?
> > 
> > Maybe I'm searching for the wrong key-words - I've looked for 
> 'white 
> > space' and 'blank lines' but not found anything. Perhaps it 
> > was even on 
> > this list?
> > 
> > Check this link for a filter solution:
> > 
> > http://www.mail-archive.com/[EMAIL PROTECTED]/ms
> > g03824.html
> > 
> > On 02/20/2004 08:14 PM Gopalakrishnan, Jayesh wrote:
> > > I am talking abt generated output(HTML/csv/email/whatever). If 
> > > the question was abt generated java code, nobody should care.
> > > 
> > > The generated output contains newlines for every <% %> 
> enclosure 
> > > in the JSP. And if you are generating anything other 
> > > than HTML output, thats likely to cause a problem.
> > > 
> > > We did this for our export to exel feature...
> > > 
> > > cheers
> > > -jayash
> > > 
> > > 
> > > 
> > > -----Original Message-----
> > > From: Thad Humphries [EMAIL PROTECTED]
> > > Sent: Friday, February 20, 2004 11:05 AM
> > > To: Struts Users Mailing List
> > > Subject: Re: Extra Lines in Compiled JSP
> > > 
> > > 
> > > Why care about how generated code looks?  This approach 
> > makes the code you
> > 
> > > maintain harder to read.
> > > 
> > > On Friday 20 February 2004 14:01, Gopalakrishnan, Jayesh wrote:
> > > 
> > >>I must have had the same dream !!
> > >>I remember that tomcat config thing......and not too
> > >>long ago. I am surprised you couldn't find it.
> > >>
> > >>We had to do something like this though, for weblogic.
> > >>
> > >><%@ taglib uri="xxx" prefix="bean"
> > >>%><%@ taglib uri="/display" prefix="display"
> > >>%><%@ taglib uri="xxx" prefix="html"
> > >>%><%@ taglib uri="xxx" prefix="nested"
> > >>%><%@ taglib uri="xxx" prefix="tiles"
> > >>%><%@ page language="java"
> > >>%><%@ page contentType="text/html; charset=UTF-8"
> > >>%><% your code continues.....%>
> > >>...
> > > 
> > > 
> > > 
> > > 
> > -----------------------------------------------------------------
> ----
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: struts-user-
> [EMAIL PROTECTED]> > 
> > > 
> > -----------------------------------------------------------------
> ----
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: struts-user-
> [EMAIL PROTECTED]> > 
> > > 
> > 
> > 
> > -- 
> > struts 1.1 + tomcat 5.0.16 + java 1.4.2
> > Linux 2.4.20 Debian
> > 
> > 
> > -----------------------------------------------------------------
> ----
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > 
> > 
> 
> This transmission (and any information attached to it) may be 
> confidential and
> is intended solely for the use of the individual or entity to 
> which it is
> addressed. If you are not the intended recipient or the person 
> responsible for
> delivering the transmission to the intended recipient, be advised 
> that you
> have received this transmission in error and that any use, 
> dissemination,forwarding, printing, or copying of this information 
> is strictly prohibited.
> If you have received this transmission in error, please 
> immediately notify
> LabOne at the following email address: 
> [EMAIL PROTECTED]
> 
> -------------------------------------------------------------------
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to