Re: Better handling of template exceptions in web applications.

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Monday, August 1, 2011, 6:44:03 AM, Geoff Shuetrim wrote:

> Daniel,
>
> Thanks for the detailed response.  It works a treat.  The
> additional notes about flushing issues are particularly helpful.  In
> the end I modified the process() method of a copy of the 
> FreemarkerServlet class.  I already have a custom extension of the
> FreemarkerServlet class and my first attempt involved copying the
> process method from the original FreemarkerServlet class,
> distributed with Freemarker, into my own custom Servlet but
> unfortunately, the process method in the FreemarkerServlet class
> depends on package-private access to methods of the
> freemarker.ext.servlet.HTTPSessionHashModel.  Because of that, I
> have duplicated the whole FreemarkerServlet and HTTPSessionHashModel
> classes into my own code base.

(Folks do that sometimes it seems, copying it. FreemarkerServlet has
not enough customization points it seems. OTOH modern frameworks
probably will not use it anyway.)

> Maybe there something smarter I should be doing to gain control of
> the writer being passed to the Template.process() method but this
> has got me where I needed to be.

Can't you just override FreemarkerServlet.preprocessRequest and there
call request.setBufferSize(somethingHuge)?

> Regards
>
> Geoff
>
> On 29 July 2011 19:15, Daniel Dekany <[email protected]> wrote:
> Friday, July 29, 2011, 3:24:47 AM, Geoff Shuetrim wrote:
>
> [snip]
>
>> When a template exception occurs, after making those changes, the
>> response has been committed.  The content I specify to be written to
>> the response does get written but it is at the end of a yellow stack
>> trace, which is what I have been trying to hide from users.
>>
>> Even if I can get that to work, my preference would be to use full
>> page buffering but it is not at all clear to me how I should provide
>> the Writer instance for the Template.process(...) method when
>> working in a servlet context.  The writer being used by the
>> HTTPServletResponse is accessible from the the HTTPServletResponse
>> but there is no setter for the writer in the response.
>>
>> Thoughts on how to solve the problem either way are appreciated.
>
> I suppose it's clear that you can pass whatever Writer to
> Template.process as a parameter, and FreeMarker doesn't care where did
> you get that and what it does. If you can't make the Servlet API to
> give you a full-buffering Writer (which is then not a
> FreeMarker-related issue), you can still create your own Writer that
> wraps the ServletResponse Writer, buffers everything, and flushes into
> the wrapped Writer on flush() and close(), and pass that Writer to
> FreeMarker.
>
> One thing you should be aware of is that by default FreeMarker flushed
> the writer at the end of Template.process if there was no exception.
> This can be a problem if web pages are built from multiple independent
> templates that are called with Template.process (rather than with
> #include). You can fix this with Configurable.setAutoFlush(false).
>
> Also, FreeMarker templates can call Writer.flush() with <#flush>. I
> don't think you need to suppress that, but you can, if your Writer
> simply ignores flush() calls.
>
> --
> Best regards,
>  Daniel Dekany
>

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Got Input?   Slashdot Needs You.
Take our quick survey online.  Come on, we don't ask for help often.
Plus, you'll get a chance to win $100 to spend on ThinkGeek.
http://p.sf.net/sfu/slashdot-survey
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.