velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adriana Danes <ADRIANA.DA...@EVIDIAN.COM>
Subject Using Contexts within VelocityServlets
Date Tue, 26 Mar 2002 13:26:59 GMT

I am facing the following problem :

I have a servlet (say Serv1) called from an index.html page.
This is the first servlet called when a user connects to my application.
This servlet puts some information
in the newly created context (say ctx1). Then the template engine
contsructs the response html page which
allows calling other servlets (Serv2, Serv3, ..) corresponding to
services the application provide.
The information put by Serv1 in ctx1 is also needed by the other
servlets (it has to be passed to the template engine
for constructing their responses).
So I thought that Context chaining is a solution. The other servlets
need to construct a new context this way :
         VelocityContext context = new VelocityContext(ctx1);

The question is how can Serv2 get the ctx1 object when treating a
request ?

I tested a solution based on the HttpSession object. This object is
created when a first request is treated by Serv1 :
    HttpSession session = request.getSession(true);
    if (session.isNew()) {
        session.setAttribute("ctx1", context);
Also, Serv2 (Serv3, ..) implement createConext() containing the
following :
    HttpSession session = request.getSession();
     VelocityContext context;
     if (session != null) {
        VelocityContext initialContext =
        if (initialContext != null) {
            context = new VelocityContext(initialContext);
What do you think about this solution ? Has somebody a better idea ?


View raw message