velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Payne, Matthew" <>
Subject RE: Mixing jsp content with tiles using tiles tool(possble soluti on!!!!)
Date Thu, 02 Oct 2003 03:25:53 GMT
I found a solution/fix.  Please take a look at this post.

In short -----------------------------------------------------------

you can't call response.getOutputStream() if something else called 
response.getWriter() in the same request.

You will get an IllegalStateExeption.

jsp calls response.getOutputStream(), lets be compatible with them


In light of this, I modified the mergeTemplate() method in
VelocityViewServlet by 

//ServletOutputStream output = response.getOutputStream();


PrintWriter pw = response.getWriter(); (and commented out corresponding

This appears to work very well.  I tested this on tomcat and resin.

By using response.getWriter(), this simple change would make the
VelocityServlet "more" compatible with jsp.
This should enabled velocity to be have better interchangeability with
Mixed tiles definitions like this will work fine.   

 <definition name="jsplayout"
>> 		<put name="header"
>> 		     value="/header.vm"/>
>> 		<put name="footer"
>> 		     value="/footer.jsp"/>
>> 		<put name="body"
>> 		     value="/center/listThemes.vm"/>
>> 		<put name="rail"
>> 		     value="/rail.jsp"/>
>> </definition>

What do you guys think of committing this type of change to the velocity
tools (i don't have access)????

Matthew Payne

Here is a copy of the "hacked" mergeTemplate() below.

     *  merges the template with the context.  Only
override this if you really, really
     *  really need to. (And don't call us with
questions if it breaks :)
     *  @param template template object returned by
the handleRequest() method
     *  @param context  context created by the
createContext() method
     *  @param response servlet reponse (use this to
get the output stream or Writer
    protected void mergeTemplate(Template template, 
                                 Context context, 
        throws ResourceNotFoundException,
               MethodInvocationException, IOException,

               UnsupportedEncodingException, Exception
        //ServletOutputStream output = response.getOutputStream();
        PrintWriter pw = response.getWriter();
        VelocityWriter vw = null;
           /* vw = (VelocityWriter) writerPool.get();
            if (vw == null)
                vw = new VelocityWriter(new
OutputStreamWriter(output, encoding), 4*1024, true);
OutputStreamWriter(output, encoding));
            } */
            template.merge(context, pw);
                if (vw != null)
                    // flush and put back into the
                    // don't close to allow us to play
                    // nicely with others.
                    /* This hack sets the
VelocityWriter's internal ref to the 
                     * OutputStreamWriter to null to
keep memory free while
                     * the writer is pooled. See bug
report #18951 */
            catch (Exception e)
                // do nothing

-----Original Message-----
From: Nathan Bubna []
Sent: Wednesday, October 01, 2003 4:54 PM
To: Velocity Users List
Subject: Re: Mixing jsp content with tiles using tiles tool

Payne, Matthew said:
> The filter see its.  Here is the stack trace under tomcat 5.
> Notice the VelocityViewServlet is getting hit. Looks like it would be
> helpful if VelocityViewServlet had a little better error handling.  It
> should probably print a stack trace if it can't write web content.

that's precisely what the VelocityViewServlet.error(...) method was trying
do when this exception was thrown.  it apparently was not allowed to print
the output stream.

>  [Exception in:/center/listThemes.vm] null java.lang.IllegalStateException
> at
> at
> Source) at
> Source) at

you might try writing a simple extension of the VVS that logs errors instead
of (or in addition to) printing them to the output stream.  actually, that
might be something worth doing all the time.  i'm trying to remember if
there's some reason it doesn't do that now...

Nathan Bubna

To unsubscribe, e-mail:
For additional commands, e-mail:

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message