You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Peter Mutsaers <pe...@mutsaers.com> on 2001/05/15 20:18:05 UTC

Webapp initialization (tomcat 4)

Hello,

I have some questions on webapp initialization.

sorry to send this to both tomcat-user and tomcat-dev; since I use
tomcat 4 I wasn't sure where to send this to. I hope that my questions
are answered by some brand new, not yet documented features of
tomcat-4 :)


Browsing through documents, it seems like the only way to create some
initial objects (such as a JDBC connection pool) in the ServetContext
is to create a Servlet with load-on-startup, which then in its init()
method can do some initialization.

This seems a bit clumsy to me, since I don't really need the Servlet
to handle requests. I just need some hook to initialize my
ServletContext with some objects.

Are there other (better) ways to do this?

If not: can I force the Servlet to be destroyed after my init() is
ready?


Another topic: Am I correct in believing that
ServletContext.getContext(uri) is the only way to obtain stuff from
another webapps' ServletContext?


In fact what I would like is to create a JDBC Connection Pool to be
shared amongst various webapps. I could create this pool Object in one
webapp (via the clumsy Servlet load-on-startup), then copy a reference
to the ServletContexts of the other webapps. This creates some
ordering problems (the other webapps may not run until the webapp
setting up the connection pool is ready).

It would be nicer if Tomcat, before initialising the webapps, would
offer the possibility for me to run some code. This code (in my case
creating the pool) should then create some initial context that all
webapps can access (in order to create a reference in their own
ServletContext).

I was hoping that in servlet.xml, within the <Host> scope I could
specify some Object to be shared between all <Context>'s.

Is something like this available?


After a day of searching through specs, mailing-list archives etc I
hope someone can help me :)


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

Re: java.lang.IllegalStateException

Posted by Andrew Robson <an...@netscapeonline.co.uk>.
Hi Chad,
  Yes. I think more or less any time I write to WEB-INF/classes 
without re-starting tomcat I get exact same error.  
I have the same set-up but also had the same problem tomcat 3.2 and redhat 6.2

andrew

On Wed, 16 May 2001, you wrote:
> tomcat 3.2.1
> redhat 7
> 
> every now and then, we get the following error. it seems to only happen 
> when we are compiling new classes a lot during development. if we are 
> not developing, but rather, just using our application, we never get 
> this error.
> 
> has anyone seen this before?
> 
> 
> 
> Error: 500
> Location: /MQ100/servlet/MQ100Generator
> Internal Servlet Error:
> 
> java.lang.IllegalStateException: Can't happen - classname is null, who 
> added
> this ?
> 	at
> org.apache.tomcat.core.ServletWrapper.loadServlet(ServletWrapper.java:261)
> 	at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:289)
> 	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.handleStatus(ContextManager.java:1049)
> 	at
> org.apache.tomcat.facade.HttpServletResponseFacade.sendError
> (HttpServletResp
> onseFacade.java:216)
> 	at
> org.apache.tomcat.facade.HttpServletResponseFacade.sendRedirect
> (HttpServletR
> esponseFacade.java:230)
> 	at Admin.Redirect(Admin.java:46)
> 	at AddScoreCard.SelectAddScoreCard(AddScoreCard.java:143)
> 	at MQ100Generator.doGet(MQ100Generator.java:287)
> 	at MQ100Generator.doPost(MQ100Generator.java:389)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
> 	at 
> org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
> 	at org.apache.tomcat.core.Handler.service(Handler.java:286)
> 	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(Thread.java:484)
-- 
===========================================
Andrew Robson         
2/1 
723 Pollockshaws Road
Glasgow G41 2AA
 
Tel:     (0141) 424 0607
Mobile:  07759 430234 


java.lang.IllegalStateException

Posted by Chad Harrison <ch...@toshoklabs.com>.
tomcat 3.2.1
redhat 7

every now and then, we get the following error. it seems to only happen 
when we are compiling new classes a lot during development. if we are 
not developing, but rather, just using our application, we never get 
this error.

has anyone seen this before?



Error: 500
Location: /MQ100/servlet/MQ100Generator
Internal Servlet Error:

java.lang.IllegalStateException: Can't happen - classname is null, who 
added
this ?
	at
org.apache.tomcat.core.ServletWrapper.loadServlet(ServletWrapper.java:261)
	at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:289)
	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.handleStatus(ContextManager.java:1049)
	at
org.apache.tomcat.facade.HttpServletResponseFacade.sendError
(HttpServletResp
onseFacade.java:216)
	at
org.apache.tomcat.facade.HttpServletResponseFacade.sendRedirect
(HttpServletR
esponseFacade.java:230)
	at Admin.Redirect(Admin.java:46)
	at AddScoreCard.SelectAddScoreCard(AddScoreCard.java:143)
	at MQ100Generator.doGet(MQ100Generator.java:287)
	at MQ100Generator.doPost(MQ100Generator.java:389)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at 
org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
	at org.apache.tomcat.core.Handler.service(Handler.java:286)
	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(Thread.java:484)


Re: Webapp initialization (tomcat 4)

Posted by Bo Xu <bo...@cybershop.ca>.
Peter Mutsaers wrote:

> Hello,
>
> I have some questions on webapp initialization.
>
> sorry to send this to both tomcat-user and tomcat-dev; since I use
> tomcat 4 I wasn't sure where to send this to. I hope that my questions
> are answered by some brand new, not yet documented features of
> tomcat-4 :)
>
> Browsing through documents, it seems like the only way to create some
> initial objects (such as a JDBC connection pool) in the ServetContext
> is to create a Servlet with load-on-startup, which then in its init()
> method can do some initialization.
>
> This seems a bit clumsy to me, since I don't really need the Servlet
> to handle requests. I just need some hook to initialize my
> ServletContext with some objects.
>
> Are there other (better) ways to do this?
>
> If not: can I force the Servlet to be destroyed after my init() is
> ready?
>
> Another topic: Am I correct in believing that
> ServletContext.getContext(uri) is the only way to obtain stuff from
> another webapps' ServletContext?
>
> In fact what I would like is to create a JDBC Connection Pool to be
> shared amongst various webapps. I could create this pool Object in one
> webapp (via the clumsy Servlet load-on-startup), then copy a reference
> to the ServletContexts of the other webapps. This creates some
> ordering problems (the other webapps may not run until the webapp
> setting up the connection pool is ready).
>
> It would be nicer if Tomcat, before initialising the webapps, would
> offer the possibility for me to run some code. This code (in my case
> creating the pool) should then create some initial context that all
> webapps can access (in order to create a reference in their own
> ServletContext).
>
> I was hoping that in servlet.xml, within the <Host> scope I could
> specify some Object to be shared between all <Context>'s.
>
> Is something like this available?
>
> After a day of searching through specs, mailing-list archives etc I
> hope someone can help me :)
>
> --
> Peter Mutsaers  |  D�bendorf    | UNIX - Live free or die
> plm@gmx.li      |  Switzerland  | Sent via FreeBSD 4.3-stable

Hi :-)  I am not sure, the following is from servlet-2_3-pfd2-spec.pdf(for

Servlet containers which supports Servlet spec2.3):

...
SRV.10.2.2 An Example of Listener Use
To illustrate a use of the event scheme, consider a simple web application
containing
a number of servlets that make use of a database. The developer has
provided a serv-let
context listener class for management of the database connection.
1. When the application starts up, the listener class is notified. The
application
logs on to the database, and stores the connection in the servlet context.

2. Servlets in the application access the connection as needed during
activity in
the web application.
3. When the web server is shut down, or the application is removed from
the web
server, the listener class is notified and the database connection is
closed.
...


Bo
May.15, 2001




Re: Webapp initialization (tomcat 4)

Posted by Peter Mutsaers <pe...@mutsaers.com>.
>> "Glenn" == Glenn Nielsen <gl...@voyager.apg.more.net> writes:

    Glenn> Tomcat 4.0 supports JNDI and JDBC DataSource pooling.  I
    Glenn> configure a named JNDI JDBC DataSource in the
    Glenn> <DefaultContext .. />, which is then available to all web
    Glenn> applications for a Host.

    Glenn> Here is an example from my server.xml (sensitive paramater
    Glenn> data replaced with generic)

Thanks, this looks like the way to do it.

I guess I have to put my implementing class in $CATALINA_HOME/classes?
Or in $CATALINA_HOME/lib/classes?!?

Is this, and the <Resource> tag, somewhere documented? for example I'm
wondering about the auth= attribute.

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

Re: Webapp initialization (tomcat 4)

Posted by Glenn Nielsen <gl...@voyager.apg.more.net>.
Tomcat 4.0 supports JNDI and JDBC DataSource pooling.

I configure a named JNDI JDBC DataSource in the <DefaultContext .. />,
which is then available to all web applications for a Host.

Here is an example from my server.xml (sensitive paramater data replaced with generic)

        <DefaultContext debug="0" reloadable="true">

          <!-- JNDI Named JDBC DataSource -->
          <Resource name="jdbc/test" auth="CONTAINER"
                    type="javax.sql.DataSource"/>
          <ResourceParams name="jdbc/test">
            <parameter><name>user</name><value>dbuserid</value></parameter>
            <parameter><name>password</name><value>dbpasswd</value></parameter>
            <parameter><name>driverClassName</name>
              <value>org.gjt.mm.mysql.Driver</value></parameter>
            <parameter><name>driverName</name>
              <value>jdbc:mysql://localhost/test</value>
            </parameter>
          </ResourceParams>

          <!-- JNDI Named MimePartDataSource for sending email via SMTP -->
          <Resource name="mail/send" auth="CONTAINER"
                    type="javax.mail.internet.MimePartDataSource"/>
          <ResourceParams name="mail/send">
            <parameter><name>factory</name>
              <value>org.apache.naming.factory.SendMailFactory</value>
            </parameter>
            <parameter><name>smtphost</name>
              <value>your.smtp.host</value>
            </parameter>
            <parameter><name>user</name>
              <value>joeuser</value>
            </parameter>
            <parameter><name>from</name>
              <value>joeuser@some.domain</value>
            </parameter>
          </ResourceParams>

        </DefaultContext>

Regards,

Glenn

Peter Mutsaers wrote:
> 
> Hello,
> 
> I have some questions on webapp initialization.
> 
> sorry to send this to both tomcat-user and tomcat-dev; since I use
> tomcat 4 I wasn't sure where to send this to. I hope that my questions
> are answered by some brand new, not yet documented features of
> tomcat-4 :)
> 
> Browsing through documents, it seems like the only way to create some
> initial objects (such as a JDBC connection pool) in the ServetContext
> is to create a Servlet with load-on-startup, which then in its init()
> method can do some initialization.
> 
> This seems a bit clumsy to me, since I don't really need the Servlet
> to handle requests. I just need some hook to initialize my
> ServletContext with some objects.
> 
> Are there other (better) ways to do this?
> 
> If not: can I force the Servlet to be destroyed after my init() is
> ready?
> 
> Another topic: Am I correct in believing that
> ServletContext.getContext(uri) is the only way to obtain stuff from
> another webapps' ServletContext?
> 
> In fact what I would like is to create a JDBC Connection Pool to be
> shared amongst various webapps. I could create this pool Object in one
> webapp (via the clumsy Servlet load-on-startup), then copy a reference
> to the ServletContexts of the other webapps. This creates some
> ordering problems (the other webapps may not run until the webapp
> setting up the connection pool is ready).
> 
> It would be nicer if Tomcat, before initialising the webapps, would
> offer the possibility for me to run some code. This code (in my case
> creating the pool) should then create some initial context that all
> webapps can access (in order to create a reference in their own
> ServletContext).
> 
> I was hoping that in servlet.xml, within the <Host> scope I could
> specify some Object to be shared between all <Context>'s.
> 
> Is something like this available?
> 
> After a day of searching through specs, mailing-list archives etc I
> hope someone can help me :)
> 
> --
> Peter Mutsaers  |  Dübendorf    | UNIX - Live free or die
> plm@gmx.li      |  Switzerland  | Sent via FreeBSD 4.3-stable

-- 
----------------------------------------------------------------------
Glenn Nielsen             glenn@more.net | /* Spelin donut madder    |
MOREnet System Programming               |  * if iz ina coment.      |
Missouri Research and Education Network  |  */                       |
----------------------------------------------------------------------

Re: Webapp initialization (tomcat 4)

Posted by Bo Xu <bo...@cybershop.ca>.
Peter Mutsaers wrote:

> >> "Jon" == Jon Stevens <jo...@latchkey.com> writes:
>
>     Jon> You could implement a Singleton based Service. This is what
>     Jon> Turbine did. It has an entire Service framework for dealing
>     Jon> with exactly this problem. It takes care of startup and
>
> That's a good idea. I already made a Servlet now (which I also use as
> the management servlet for my application, also spawning a thread for
> some scheduled actions and controlling that thread(s)), but I might
> put the ConnectionPool in a Singleton instead.
> [...]

Hi :-) I am not sure, and I just suggest that:
perhaps it is better to let this Singleton-ConnectionPool loaded by a
classloader which is "upper" than the classloader of this webapp or
that, so perhaps we can avoid some exceptions such as
ClassCastException(for example, when reloading)


>
>  Heh, at the moment I have no boss. I have to get as much work done
> before the next one comes :)
> [...]

I am Not sure, but I find in this List, there are some people  whose
emails
 address are from hotmail or usa.net or......

I am Not sure, but is it possible that ......?   //hahahahaha :-)  (just
a fun ^_^ )



have a nice day! :-)
Bo
May.16, 2001



Re: Webapp initialization (tomcat 4)

Posted by Peter Mutsaers <pe...@mutsaers.com>.
>> "Jon" == Jon Stevens <jo...@latchkey.com> writes:

    Jon> You could implement a Singleton based Service. This is what
    Jon> Turbine did. It has an entire Service framework for dealing
    Jon> with exactly this problem. It takes care of startup and

That's a good idea. I already made a Servlet now (which I also use as
the management servlet for my application, also spawning a thread for
some scheduled actions and controlling that thread(s)), but I might
put the ConnectionPool in a Singleton instead.

    Jon> destroy. There is also a very good JDBC connection pool
    Jon> included with Turbine, however, it isn't strictly J2FooEE so
    Jon> you may have to fight those pointy haired bosses who only
    Jon> regurgitate what they read in magazines.

Heh, at the moment I have no boss. I have to get as much work done
before the next one comes :)

    Jon> <http://jakarta.apache.org/turbine/>

I'll have a look at turbine, thanks.

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

Re: Webapp initialization (tomcat 4)

Posted by Jon Stevens <jo...@latchkey.com>.
on 5/15/01 11:18 AM, "Peter Mutsaers" <pe...@mutsaers.com> wrote:

> Hello,
> 
> I have some questions on webapp initialization.
> 
> sorry to send this to both tomcat-user and tomcat-dev; since I use
> tomcat 4 I wasn't sure where to send this to. I hope that my questions
> are answered by some brand new, not yet documented features of
> tomcat-4 :)

Tomcat-user is the right place.

> Browsing through documents, it seems like the only way to create some
> initial objects (such as a JDBC connection pool) in the ServetContext
> is to create a Servlet with load-on-startup, which then in its init()
> method can do some initialization.

You could implement a Singleton based Service. This is what Turbine did. It
has an entire Service framework for dealing with exactly this problem. It
takes care of startup and destroy. There is also a very good JDBC connection
pool included with Turbine, however, it isn't strictly J2FooEE so you may
have to fight those pointy haired bosses who only regurgitate what they read
in magazines.

<http://jakarta.apache.org/turbine/>

> Another topic: Am I correct in believing that
> ServletContext.getContext(uri) is the only way to obtain stuff from
> another webapps' ServletContext?
> 
> In fact what I would like is to create a JDBC Connection Pool to be
> shared amongst various webapps. I could create this pool Object in one
> webapp (via the clumsy Servlet load-on-startup), then copy a reference
> to the ServletContexts of the other webapps. This creates some
> ordering problems (the other webapps may not run until the webapp
> setting up the connection pool is ready).

IMHO, A better approach is to have those webapps living under the same
context. Turbine's system isn't quite there yet, but it is very close. We
will have full transparent support for this in the near future.

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


Re: Webapp initialization (tomcat 4)

Posted by Jon Stevens <jo...@latchkey.com>.
on 5/15/01 11:18 AM, "Peter Mutsaers" <pe...@mutsaers.com> wrote:

> Hello,
> 
> I have some questions on webapp initialization.
> 
> sorry to send this to both tomcat-user and tomcat-dev; since I use
> tomcat 4 I wasn't sure where to send this to. I hope that my questions
> are answered by some brand new, not yet documented features of
> tomcat-4 :)

Tomcat-user is the right place.

> Browsing through documents, it seems like the only way to create some
> initial objects (such as a JDBC connection pool) in the ServetContext
> is to create a Servlet with load-on-startup, which then in its init()
> method can do some initialization.

You could implement a Singleton based Service. This is what Turbine did. It
has an entire Service framework for dealing with exactly this problem. It
takes care of startup and destroy. There is also a very good JDBC connection
pool included with Turbine, however, it isn't strictly J2FooEE so you may
have to fight those pointy haired bosses who only regurgitate what they read
in magazines.

<http://jakarta.apache.org/turbine/>

> Another topic: Am I correct in believing that
> ServletContext.getContext(uri) is the only way to obtain stuff from
> another webapps' ServletContext?
> 
> In fact what I would like is to create a JDBC Connection Pool to be
> shared amongst various webapps. I could create this pool Object in one
> webapp (via the clumsy Servlet load-on-startup), then copy a reference
> to the ServletContexts of the other webapps. This creates some
> ordering problems (the other webapps may not run until the webapp
> setting up the connection pool is ready).

IMHO, A better approach is to have those webapps living under the same
context. Turbine's system isn't quite there yet, but it is very close. We
will have full transparent support for this in the near future.

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