You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by pl...@mutsaers.com on 2001/06/21 19:09:21 UTC

servlet using multiple contexts and templates

Hello,

If I need to include a template multiple times from my VelocityServlet
derived servlet, while modifying the context, what is the best way to
do it?


The standard model (handleRequest) assumes you use only 1 template and
1 context.

But I need to output a table.vm N times (N not known in advance), so
my servlet would, so it seems, have to override doRequest() so that I
can call mergeTemplate multiple times (such as in a for(i=0; i<N; i++)
loop). Just wanted to check if this is true or if I'm missing
something.


Btw, my output can get megabyte large. It must be sent back to the
browser (poor browser, but I'd rather have the browser struggle than
my servlet engine :) immediately, the full output may not be buffered
or kept in memory in the servlet:

Does Template.merge(Context, VelocityWriter) flush immediately, and
can I call it N times resulting in the concatenation of each result
being sent on the ServletOutputStream during the process?



-- 
Peter Mutsaers  |  Dübendorf    | UNIX - Live free or die
plm@gmx.li      |  Switzerland  | Sent via FreeBSD 4.3-stable

Re: servlet using multiple contexts and templates

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Peter Mutsaers wrote:
> 
> Some tests returning 1000 templates showed this works wonderfully and
> fast, sending back lots of mergeTemplate-outputs to the browser works
> fine without overloading the servlet memory.

Poor browser...
 
> By the way, I switched two days ago from JSP to Velocity, built a
> little framework with controllers and beans (model/data) and I'm very
> impressed and happy I made the switch. I was already 'templatizing' our
> old JSP's, that is, give control to a servlet and then dispatch to
> included JSP's. That is possible, but working in that mode JSP adds a
> lot of unnecessary overhead and complexity that just is not necessary
> nor useful. Thanks to the Velocity developers

Sounds great!

geir
-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: servlet using multiple contexts and templates

Posted by Peter Mutsaers <pe...@mutsaers.com>.
>> "Geir" == Geir Magnusson <ge...@optonline.net> writes:

    Geir> You can either take care of it in the template (like Nathan
    Geir> suggested), or if this is how your application always works,
    Geir> you can do what you want in the servlet - it's your choice.
    Geir> The VelocityServet is really a convenience for the majority
    Geir> of users - for something like this, you can see how it works
    Geir> and roll your own.  You have the source :)

Yes, I have made my own Subclass of VelocityServlet that uses
mergeTemplate in a loop over an Iterator (a "Controller" Interface
that extends Iterator) to repeatedly merge the context (each time the
same, but the Iterator-implementor can do what he want before each
next() call to modify the context) with different templates. The
next() call is supposed to return a template-name.

The default Controller (ControllerBase class) implements it so that
only once a template is returned, which makes for behaviour like the
standard VelocityServlet. Subclass-controllers can however override
next() and hasNext() to return one template after the other (even
deciding at run-time depending on external factors which templates to
return).

Some tests returning 1000 templates showed this works wonderfully and
fast, sending back lots of mergeTemplate-outputs to the browser works
fine without overloading the servlet memory.


By the way, I switched two days ago from JSP to Velocity, built a
little framework with controllers and beans (model/data) and I'm very
impressed and happy I made the switch. I was already 'templatizing' our
old JSP's, that is, give control to a servlet and then dispatch to
included JSP's. That is possible, but working in that mode JSP adds a
lot of unnecessary overhead and complexity that just is not necessary
nor useful. Thanks to the Velocity developers


-- 
Peter Mutsaers  |  Dübendorf    | UNIX - Live free or die
plm@gmx.li      |  Switzerland  | Sent via FreeBSD 4.3-stable

Re: servlet using multiple contexts and templates

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
plm@mutsaers.com wrote:
> 
> Hello,
> 
> If I need to include a template multiple times from my VelocityServlet
> derived servlet, while modifying the context, what is the best way to
> do it?
> 
> The standard model (handleRequest) assumes you use only 1 template and
> 1 context.
> 
> But I need to output a table.vm N times (N not known in advance), so
> my servlet would, so it seems, have to override doRequest() so that I
> can call mergeTemplate multiple times (such as in a for(i=0; i<N; i++)
> loop). Just wanted to check if this is true or if I'm missing
> something.
 
You can either take care of it in the template (like Nathan suggested),
or if this is how your application always works, you can do what you
want in the servlet - it's your choice.  The VelocityServet is really a
convenience for the majority of users - for something like this, you can
see how it works and roll your own.
You have the source :)

> Btw, my output can get megabyte large. It must be sent back to the
> browser (poor browser, but I'd rather have the browser struggle than
> my servlet engine :) immediately, the full output may not be buffered
> or kept in memory in the servlet:

Goodness.  Can the user really see that?  can you make your servlet
write a file and get the browser to download it?
 
> Does Template.merge(Context, VelocityWriter) flush immediately, and
> can I call it N times resulting in the concatenation of each result
> being sent on the ServletOutputStream during the process?

If you use the VelocityWriter, it will flush when done.  Take a look at
mergeTemplate() in VelocityServlet to see how that works.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: servlet using multiple contexts and templates

Posted by Nathan Bubna <na...@esha.com>.
it sounds like this maybe a good place to use the #parse("template.vm")
function.
just put the call in a #foreach loop.

mytables.vm
-----------------------------

#foreach ($table in $mytables)
    #parse("table.vm")
#end

-----------------------------

Nathan Bubna
Esha Research

----- Original Message -----
From: <pl...@mutsaers.com>
To: <ve...@jakarta.apache.org>
Sent: Thursday, June 21, 2001 10:09 AM
Subject: servlet using multiple contexts and templates


> Hello,
>
> If I need to include a template multiple times from my VelocityServlet
> derived servlet, while modifying the context, what is the best way to
> do it?
>
>
> The standard model (handleRequest) assumes you use only 1 template and
> 1 context.
>
> But I need to output a table.vm N times (N not known in advance), so
> my servlet would, so it seems, have to override doRequest() so that I
> can call mergeTemplate multiple times (such as in a for(i=0; i<N; i++)
> loop). Just wanted to check if this is true or if I'm missing
> something.
>
>
> Btw, my output can get megabyte large. It must be sent back to the
> browser (poor browser, but I'd rather have the browser struggle than
> my servlet engine :) immediately, the full output may not be buffered
> or kept in memory in the servlet:
>
> Does Template.merge(Context, VelocityWriter) flush immediately, and
> can I call it N times resulting in the concatenation of each result
> being sent on the ServletOutputStream during the process?
>
>
>
> --
> Peter Mutsaers  |  Dübendorf    | UNIX - Live free or die
> plm@gmx.li      |  Switzerland  | Sent via FreeBSD 4.3-stable
>


Re: servlet using multiple contexts and templates

Posted by Jon Stevens <jo...@latchkey.com>.
on 6/21/01 10:09 AM, "plm@mutsaers.com" <pl...@mutsaers.com> wrote:

> Does Template.merge(Context, VelocityWriter) flush immediately, and
> can I call it N times resulting in the concatenation of each result
> being sent on the ServletOutputStream during the process?

You will need to pass in a custom Writer that knows to flush when you want
it to...

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>