You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by David Esposito <es...@newnetco.com> on 2001/02/28 15:38:59 UTC
merging context
Umm, this may be a cross between a feature request and a "how should I do
it" sort of question ... ;)
In my init() method of my servlet, I create an instance of a VelocityContext
and I fill in a bunch of default variables that I want available to my form
...
class MyServlet extends VelocityServlet
{
Context defaultContext;
void init()
{
defaultContext = new VelocityContext();
defaultContext.put("imageURL","http://images.xyz.com");
defaultContext.put("states",stateTable);
}
}
but then in my handleRequest method, I want to merge this defaultContext
with the Context parameter that is passed to handleRequest() ...
my question is, it doesn't appear from the docs that it's possible to take
two pre-existing Contexts and merge them together ... it seems like it's
only possible to create a new context using an existing context as an
initialization parameter ...
it seems like the mergeTemplate() method won't help me much either because
it only accepts one context ... it would seem like something like the
following would be the best way to handle it ..
handleRequest(Context ctx)
{
ctx.append(defaultContext);
Template tm = getTemplate("myform.vm");
return tm;
}
any ideas?
Re: merging context
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Ah, sorry - I thought you were just being lazy, not including the arg in
your post.
sounds great :)
geir
--
Geir Magnusson Jr. geirm@optonline.com
Developing for the web? See http://jakarta.apache.org/velocity/
Re: merging context
Posted by Rafal Krzewski <Ra...@e-point.pl>.
David Esposito wrote:
> I'm actually overriding the init() method that takes no arguments ... I
> don't have the java source to confirm it, but I believe, according to the
> J2EE javadocs, that the init() without args automatically calls
> super.init(config) ... From the docs on init():
>
> "A convenience method which can be overridden so that there's no need to
> call super.init(config). "
Actually it's the other way around:
Defalut implementation of init(ServletConfig) calls init(). Threrefore,
if you are overriding init(ServletConfig) you MUST call
super.init(config).
If you are overriding init(), you don't neet to worry about it.
Rafal
--
Rafal Krzewski
Senior Internet Developer
mailto:Rafal.Krzewski@e-point.pl
+48 22 8534830 http://e-point.pl
RE: merging context
Posted by David Esposito <es...@newnetco.com>.
Hehe ...
I'm actually overriding the init() method that takes no arguments ... I
don't have the java source to confirm it, but I believe, according to the
J2EE javadocs, that the init() without args automatically calls
super.init(config) ... From the docs on init():
"A convenience method which can be overridden so that there's no need to
call super.init(config). "
I think this actually works because I make several calls to
getServletContext().getInitParameter() in my init() method and it actually
gets the configuration data ...
I know this is more of a Java issue, not a Velocity issue ...
> -----Original Message-----
> From: gmj@mta6.srv.hcvlny.cv.net [mailto:gmj@mta6.srv.hcvlny.cv.net]On
> Behalf Of Geir Magnusson Jr.
> Sent: Wednesday, February 28, 2001 11:06 AM
> To: velocity-user@jakarta.apache.org
> Subject: Re: merging context
>
>
> David Esposito wrote:
> >
> > That does do it ... thanks ...
> >
> > regarding the concurrency issue ... correct me if i'm wrong here, but
> > creating the Context in the init() method only happens when the Servlet
> > instance is created ... from that point forward, all of my
> handleRequest()
> > methods are just doing read-only operations on my
> defaultContext (i would
> > never want to have anything write to my defaultContext during the
> > handleRequest method) ... i agree, that would be bad ...
> >
> > is my logic sound?
>
> Yep - that was the concern - that the defaultContext might not only be
> read-only - I know a few seasoned programmers bitten by forgetting a
> single instance of the class will be shared (including myself :)
>
> Also, I assume you are calling super.init( config ) in your init() so
> Velocity gets set up correctly?
>
> geir
>
> --
> Geir Magnusson Jr. geirm@optonline.com
> Developing for the web? See http://jakarta.apache.org/velocity/
Re: merging context
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Esposito wrote:
>
> That does do it ... thanks ...
>
> regarding the concurrency issue ... correct me if i'm wrong here, but
> creating the Context in the init() method only happens when the Servlet
> instance is created ... from that point forward, all of my handleRequest()
> methods are just doing read-only operations on my defaultContext (i would
> never want to have anything write to my defaultContext during the
> handleRequest method) ... i agree, that would be bad ...
>
> is my logic sound?
Yep - that was the concern - that the defaultContext might not only be
read-only - I know a few seasoned programmers bitten by forgetting a
single instance of the class will be shared (including myself :)
Also, I assume you are calling super.init( config ) in your init() so
Velocity gets set up correctly?
geir
--
Geir Magnusson Jr. geirm@optonline.com
Developing for the web? See http://jakarta.apache.org/velocity/
RE: merging context
Posted by David Esposito <es...@newnetco.com>.
That does do it ... thanks ...
regarding the concurrency issue ... correct me if i'm wrong here, but
creating the Context in the init() method only happens when the Servlet
instance is created ... from that point forward, all of my handleRequest()
methods are just doing read-only operations on my defaultContext (i would
never want to have anything write to my defaultContext during the
handleRequest method) ... i agree, that would be bad ...
is my logic sound?
> -----Original Message-----
> From: gmj@mta4.srv.hcvlny.cv.net [mailto:gmj@mta4.srv.hcvlny.cv.net]On
> Behalf Of Geir Magnusson Jr.
> Sent: Wednesday, February 28, 2001 10:48 AM
> To: velocity-user@jakarta.apache.org
> Subject: Re: merging context
>
>
> David Esposito wrote:
> >
> > Umm, this may be a cross between a feature request and a "how
> should I do
> > it" sort of question ... ;)
> [SNIP]
> > my question is, it doesn't appear from the docs that it's
> possible to take
> > two pre-existing Contexts and merge them together ... it seems like it's
> > only possible to create a new context using an existing context as an
> > initialization parameter ...
>
> First : be *very* careful with this - I assume you do realize that the
> defaultContext that you are creating and filling in init() will be
> shared, concurrently, across multiple threads.
>
> I personally would just add those two things in handleRequest() to the
> context provided, but I tend to be a chicken :)
>
> But, you can do it. We have overridable methods in VelocityServlet
> exactly for this purpose :
>
> override the
>
> protected Context createContext(HttpServletRequest request,
> HttpServletResponse response )
>
> method of Velocity servlet class (this is what it's there for :)
>
> Do something like :
>
> protected Context createContext(HttpServletRequest request,
> HttpServletResponse response )
> {
>
> /* chain/wrap it */
>
> VelocityContext context = new VelocityContext(defaultContext);
>
> /*
> * put the request/response objects into the context
> */
>
> context.put( REQUEST, request );
> context.put( RESPONSE, response );
>
> return context;
> }
>
> and "imageURL" and "states" will be available to the template.
>
> Does that do it?
>
> geir
>
> >
> > it seems like the mergeTemplate() method won't help me much
> either because
> > it only accepts one context ... it would seem like something like the
> > following would be the best way to handle it ..
> >
> > handleRequest(Context ctx)
> > {
> >
> > ctx.append(defaultContext);
> >
> > Template tm = getTemplate("myform.vm");
> >
> > return tm;
> > }
> >
> > any ideas?
>
> --
> Geir Magnusson Jr. geirm@optonline.com
> Developing for the web? See http://jakarta.apache.org/velocity/
Re: merging context
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Esposito wrote:
>
> Umm, this may be a cross between a feature request and a "how should I do
> it" sort of question ... ;)
[SNIP]
> my question is, it doesn't appear from the docs that it's possible to take
> two pre-existing Contexts and merge them together ... it seems like it's
> only possible to create a new context using an existing context as an
> initialization parameter ...
First : be *very* careful with this - I assume you do realize that the
defaultContext that you are creating and filling in init() will be
shared, concurrently, across multiple threads.
I personally would just add those two things in handleRequest() to the
context provided, but I tend to be a chicken :)
But, you can do it. We have overridable methods in VelocityServlet
exactly for this purpose :
override the
protected Context createContext(HttpServletRequest request,
HttpServletResponse response )
method of Velocity servlet class (this is what it's there for :)
Do something like :
protected Context createContext(HttpServletRequest request,
HttpServletResponse response )
{
/* chain/wrap it */
VelocityContext context = new VelocityContext(defaultContext);
/*
* put the request/response objects into the context
*/
context.put( REQUEST, request );
context.put( RESPONSE, response );
return context;
}
and "imageURL" and "states" will be available to the template.
Does that do it?
geir
>
> it seems like the mergeTemplate() method won't help me much either because
> it only accepts one context ... it would seem like something like the
> following would be the best way to handle it ..
>
> handleRequest(Context ctx)
> {
>
> ctx.append(defaultContext);
>
> Template tm = getTemplate("myform.vm");
>
> return tm;
> }
>
> any ideas?
--
Geir Magnusson Jr. geirm@optonline.com
Developing for the web? See http://jakarta.apache.org/velocity/