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/03/06 16:10:53 UTC

uhm, it seems like there's a problem

When I just ran all of my tests on last night's build, I was only testing
one of my servlets ... so it ran init() only once ... but I just tried to
run another servlet and I get a

javax.servlet.ServletException: Error configuring the loader:
java.lang.ClassCastException: default.contentType doesn't map to a String
object
	at
org.apache.velocity.servlet.VelocityServlet.init(VelocityServlet.java:194)
	at org.apache.tomcat.core.ServletWrapper.doInit(ServletWrapper.java:317)
	at org.apache.tomcat.core.Handler.init(Handler.java:215)
	at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:296)
	at org.apache.tomcat.core.Handler.service(Handler.java:254)
	at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
	at
org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:79
7)
	at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
	at
org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpC
onnectionHandler.java:210)
	at
org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
	at
org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
	at java.lang.Thread.run(Unknown Source)


If I stop and restart my JVM, each servlet works fine ... it seems like
there is something wrong with the way that the first call to init() leaves
velocity and therefore it crashes on each subsequent call to Velocity's
init() ...





Re: uhm, it seems like there's a problem

Posted by Jason van Zyl <jv...@periapt.com>.
>

Hmm. I saw this problem while testing and noticed the same type of
behaviour when I discovered the tested was initializing the Runtime twice.

The Configuration class when seeing a property once turns it into a
string, when seeing it twice assumes you have a collection of same
named keys and collects them in a vector.

If you send me your code, I will take a look at it. I would like to track
this down.


> When I just ran all of my tests on last night's build, I was only testing
> one of my servlets ... so it ran init() only once ... but I just tried to
> run another servlet and I get a
>
> javax.servlet.ServletException: Error configuring the loader:
> java.lang.ClassCastException: default.contentType doesn't map to a String
> object
>         at
> org.apache.velocity.servlet.VelocityServlet.init(VelocityServlet.java:194)
>         at org.apache.tomcat.core.ServletWrapper.doInit(ServletWrapper.java:317)
>         at org.apache.tomcat.core.Handler.init(Handler.java:215)
>         at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:296)
>         at org.apache.tomcat.core.Handler.service(Handler.java:254)
>         at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
>         at
> org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:79
> 7)
>         at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
>         at
> org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpC
> onnectionHandler.java:210)
>         at
> org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
>         at
> org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
>         at java.lang.Thread.run(Unknown Source)
>
> If I stop and restart my JVM, each servlet works fine ... it seems like
> there is something wrong with the way that the first call to init() leaves
> velocity and therefore it crashes on each subsequent call to Velocity's
> init() ...

--
jvz.

Jason van Zyl
jvanzyl@periapt.com

http://jakarta.apache.org/velocity
http://jakarta.apache.org/turbine




Re: uhm, it seems like there's a problem

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Esposito wrote:
> 
> When I just ran all of my tests on last night's build, I was only testing
> one of my servlets ... so it ran init() only once ... but I just tried to
> run another servlet and I get a
> 
> javax.servlet.ServletException: Error configuring the loader:
> [SNIP]

I just checked in a small fix to Runtime to solve this - protects from
repeated init().  David verified that the fix works, and Jason knows
about it from his end - so everything works now, AFAIK.

geir

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

Re: feature suggestion

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Esposito wrote:
> 
> Ahh ... that one servlet handles the whole init() thing for velocity ...
> Because of this, each additional servlet defined in the web.xml doesn't need
> the <init-param> for the properties file? And velocity won't spaz out,
> crash, or have any other undesireable behavior using this technique?
> 
> It's a shade less elegant than what I proposed, but it seems pretty
> reasonable ... ;)

The point here is that in a webapp, there is only one instance of the
Velocity runtime.  (There can be other webapps, and they will have their
own instance... so you can configure each webapp differently...) 

You can have 10 servlets calling init() 10 times, and it won't make a
difference - the first init() is what sets up Velocity.

So, using the <load-on-startup> technique means that you will have
everything set up before the servlet container starts sending requests
to your servlets.  You don't have to use one of your 'worker' servlets -
you can make one just for this purpose, and keep all of your application
init stuff in there - you are guaranteed that it is invoked before any
other servlet gets a request.

geir



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

RE: feature suggestion

Posted by David Esposito <es...@newnetco.com>.
Ahh ... that one servlet handles the whole init() thing for velocity ...
Because of this, each additional servlet defined in the web.xml doesn't need
the <init-param> for the properties file? And velocity won't spaz out,
crash, or have any other undesireable behavior using this technique?

It's a shade less elegant than what I proposed, but it seems pretty
reasonable ... ;)

> -----Original Message-----
> From: gmj@mta4.srv.hcvlny.cv.net [mailto:gmj@mta4.srv.hcvlny.cv.net]On
> Behalf Of Geir Magnusson Jr.
> Sent: Tuesday, March 06, 2001 1:27 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: feature suggestion
>
>
> David Esposito wrote:
> >
> > Can you have the init() method default to a "properties" init
> param in the
> > context if one is not defined in the servlet tag? That is, what
> I've done
> > for most of my application is put all of my parameters in the
> context ...
>
>
> [SNIP]
>
> I am probably not answering your question, but I think I know what you
> want, and there is a conventional way to do it that doesn't require any
> code change.
>
> You don't need it in every servlet in the descriptor. Just do it in one,
> and put in
>
>   <load-on-startup>1</load-on-startup>
>
> and the that servlet will be instantiated and have it's init(
> ServletConfig ) method called. Since you only need one call to
> Runtime.init() to configure Vel, this works just fine.
>
> I use this myself in production, cuz the whole <param> thing got boring
> :)
>
> ex :
> <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>
>
>
> Does that solve it?
>
> geir
>
>
>
>
> --
> Geir Magnusson Jr.                               geirm@optonline.com
> Developing for the web?  See http://jakarta.apache.org/velocity/


Re: feature suggestion

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Esposito wrote:
> 
> Can you have the init() method default to a "properties" init param in the
> context if one is not defined in the servlet tag? That is, what I've done
> for most of my application is put all of my parameters in the context ...


[SNIP]

I am probably not answering your question, but I think I know what you
want, and there is a conventional way to do it that doesn't require any
code change.

You don't need it in every servlet in the descriptor. Just do it in one,
and put in 

  <load-on-startup>1</load-on-startup>

and the that servlet will be instantiated and have it's init(
ServletConfig ) method called. Since you only need one call to
Runtime.init() to configure Vel, this works just fine.

I use this myself in production, cuz the whole <param> thing got boring
:)

ex :
<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>


Does that solve it?

geir




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

feature suggestion

Posted by David Esposito <es...@newnetco.com>.
Can you have the init() method default to a "properties" init param in the
context if one is not defined in the servlet tag? That is, what I've done
for most of my application is put all of my parameters in the context ...
like so:

	<servlet>
		<servlet-name>Logout</servlet-name>
		<servlet-class>com.servlets.LogoutServlet</servlet-class>
		<init-param>
			<param-name>properties</param-name>
			<param-value>velocity.properties</param-value>
		</init-param>
		<init-param>
			<param-name>PROVIDER_URL</param-name>
			<param-value>localhost:5999</param-value>
		</init-param>
	</servlet>

	<servlet>
		<servlet-name>SessionExpired</servlet-name>
		<servlet-class>com.servlets.SessionExpiredServlet</servlet-class>
		<init-param>
			<param-name>properties</param-name>
			<param-value>velocity.properties</param-value>
		</init-param>
	</servlet>

	<context-param>
		<description>Location of Naming server</description>
		<param-name>PROVIDER_URL</param-name>
		<param-value>localhost:1099</param-value>
	</context-param>

	<context-param>
		<description>Location of Context factory</description>
		<param-name>INITIAL_CONTEXT_FACTORY</param-name>
		<param-value>org.jnp.interfaces.NamingContextFactory</param-value>
	</context-param>

	<context-param>
		<description>Prefix for naming packages</description>
		<param-name>URL_PKG_PREFIXES</param-name>
		<param-value>org.jboss.naming</param-value>
	</context-param>

Where it uses the localhost:5999 for the LogoutServlet and use the
localhost:1099 for the SessionExpiredServlet ... that would neaten up the
deployment descriptor as then I could put the velocity.properties value into
the context instead of needing it declared for each servlet ... I've used
this method in the past and it seems to work:

private String getInitParam(GenericServlet servlet, String parameterName)
{
	String retVal = null;

	//First check to see if the value was specified on a servlet-specific basis
	if(servlet.getServletConfig().getInitParameter(parameterName) != null)
		retVal = servlet.getServletConfig().getInitParameter(parameterName);
	//Else, let's see if its set on an context-wide basis
	else
		retVal = servlet.getServletContext().getInitParameter(parameterName);

	return retVal;
}


Re: uhm, it seems like there's a problem

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Hm - There is only one instance of the Velocity Runtime in a servlet's
webapp - you should be able to call init() until the cows come home -
only the first should 'take' and the rest should do nothing.

I assume that you are using something like Tomcat 3.2 or so, and those
servlets were in the same webapp?

I will make sure my tree is refreshed, and see what's up...

Thanks.

geir

David Esposito wrote:
> 
> When I just ran all of my tests on last night's build, I was only testing
> one of my servlets ... so it ran init() only once ... but I just tried to
> run another servlet and I get a
> 
> javax.servlet.ServletException: Error configuring the loader:
> java.lang.ClassCastException: default.contentType doesn't map to a String
> object
>         at
> org.apache.velocity.servlet.VelocityServlet.init(VelocityServlet.java:194)
>         at org.apache.tomcat.core.ServletWrapper.doInit(ServletWrapper.java:317)
>         at org.apache.tomcat.core.Handler.init(Handler.java:215)
>         at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:296)
>         at org.apache.tomcat.core.Handler.service(Handler.java:254)
>         at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
>         at
> org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:79
> 7)
>         at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
>         at
> org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpC
> onnectionHandler.java:210)
>         at
> org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
>         at
> org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
>         at java.lang.Thread.run(Unknown Source)
> 
> If I stop and restart my JVM, each servlet works fine ... it seems like
> there is something wrong with the way that the first call to init() leaves
> velocity and therefore it crashes on each subsequent call to Velocity's
> init() ...

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