You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Thomas Fahrmeyer <th...@einsurance.de> on 2001/03/09 10:22:22 UTC

Init problems with more than one WebApp !!

Hi,

I'm not sure whether my problem is coverd by the running "Configuration
issues" discussion in the vel-dev list.

I have a Velocity nightly build from 02/09. I have two webapps (Tomcat
3.2.1) and both wanna using Velocity. In each Webapp is a
InitVelocityServlet which is defined as "load-on-startup" to initalize the
Vel. Runtime. It has the "properties" parameter.

There are no context definitions in Tomcat's server.xml. All webapps are
loaded and therefore the InitServlet is loaded twice which causes
Runtime.init(propfilename) called twice too. But the second call is ignored
(Runtime is already initialized).
Therefore the templates for the second webapp can not be found because the
template path was set from the first webapp.

How can I resolve this problem ? Is this still existing in the current
source ?

Thanx for your help ;)

bye
Thomas



Re: Init problems with more than one WebApp !!

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Thomas Fahrmeyer wrote:
> 
> Hi,
> 
> I'm not sure whether my problem is coverd by the running "Configuration
> issues" discussion in the vel-dev list.

No. But thanks for reading it.  Jason and I were hoping that it was
entertaining for someone :)

> I have a Velocity nightly build from 02/09. I have two webapps (Tomcat
> 3.2.1) and both wanna using Velocity. In each Webapp is a
> InitVelocityServlet which is defined as "load-on-startup" to initalize the
> Vel. Runtime. It has the "properties" parameter.

Here's what to do.  I assume that you really have two webapps, each with
their own directory under Tomcat's webapps directory. If not, do this.

1) put velocity-0.X.jar in the WEB-INF/lib directory of *EACH* webapp on
your server :

webapps/<app1>/WEB-INF/lib/velocity-0.X.jar
webapps/<app2>/WEB-INF/lib/velocity-0.X.jar

and remove from anywhere else (such as tomcats lib directory)

2) put a properties file, say velocity.properties in *EACH* webapp's
root directory

webapps/<app1>/velocity.properties
webapps/<app2>/velocity.properties

and specify the appropriate template directory for each with the
resource.loader...resource.path =  property.  For now, you can make them
absolute. (I will leave it as an exercise for the reader how do make it
relative - it doesn't matter)

3) In the WEB-INF/web.xml file in *EACH* webapp, add

<servlet>
    <servlet-name>ContentCtrl</servlet-name>
    <servlet-class>com.ridersonline.servlet.ContentCtrl</servlet-class>
    <init-param>
      <param-name>properties</param-name>
      <param-value>velocity.properties</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

(and use your own servlet, of course)

4) bounce Mr. Tomcat

This should work just fine (I do it all the time :)

What you are doing is taking advantage of the fine feature of Servlet
Spec 2.2 that Tomcat supports (correctly!) and relying on there being a
'private' classloader for each webapp.  So then each webapp will have
it's own Velocity Runtime class, each configured differently.

Yell if it doesn't work.

> There are no context definitions in Tomcat's server.xml. All webapps are
> loaded and therefore the InitServlet is loaded twice which causes
> Runtime.init(propfilename) called twice too. But the second call is ignored
> (Runtime is already initialized).

You don't have to touch Tomcat's server.xml for this.

Note that it's ok that in the config above, each servlet calls init() :
you want that, because you have two Runtime's, so each init() does
matter.

geir

-- 
Geir Magnusson Jr.                               geirm@optonline.net
Developing for the web?  See http://jakarta.apache.org/velocity/