File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes server.xml quirk with startup servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "server.xml quirk with startup servlet" Watch "server.xml quirk with startup servlet" New topic
Author

server.xml quirk with startup servlet

sneaky thesnafu
Greenhorn

Joined: Jan 16, 2005
Posts: 4
I've been trying to solve this problem now for awhile and its driving me nuts. I have a webapp that needs to initialize a bunch of data on startup so that it can be cached for easy access. To accomplish this I created a startup servlet that is defined in my web.xml's <load-on-startup>. I also startup log4j in the startup servlet so I can check that all the data is initializing correctly. So far so good, everything works exactly the way its supposed to with one exception...

The minute I include a <Context> element in my server.xml file, tomcat will execute the code in the startup servlet twice! For every additional context I include, it will hit the startup servlet an additional time. This makes no sense to me because the context is supposed to represent an instance of a single webapp (say its called 'mywebapp1')...other contexts that reference other webapps shouldnt call mywebapp1's startup, that makes no sense!

My server.xml looks like this

<Service name="Catalina">
<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8080" redirectPort="8443"/>
<Connector port="8009" protocol="AJP/1.3" protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8443"/>

<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps">
<Context path="" docBase="web-www" crossContext="false" debug="1" reloadable="true" >

<Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/>
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
</Context>
</Host>
</Engine>
</Service>

Please help, this is driving me crazy
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Set autoDeploy="false" in the Host node of your server.xml.

Tomcat is deploying one app with the path="" as you've instructed in your Context node and autodeploying the "web-www" app because it's under the webapps directory.

Three other points:
1.) The documentation for the current versions of Tomcat no longer recommends creating a Context node in server.xml. You can now put an xml fragment in the conf/Catalina directory to achive the same results.

2.) Starting with Servlet Spec 2.4, ContextListeners are available for initializing applications on startups. It's cleaner than using a servlet to initialize an application.

3.) There's no need to "startup" log4j. The results of Logger.getLogger(String) can be a static variable.
[ January 16, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
sneaky thesnafu
Greenhorn

Joined: Jan 16, 2005
Posts: 4
hmmm, I was excited when I read your post because I thought "oh of course" that autoDeploy defaults to true that's probably exactly the problem. Unfortunately it made no difference. As to your other points:

1)moving the context to an context.xml fragment in the conf/Catalina/localhost directory.

tried it, same results

2)I havent looked into the listeners, but you're probably right that its cleaner...not sure that it would solve my issue though.

3)"startup" wasnt the right choice of words, all I was saying was that I was grabbing a logger reference in my startup class so I could log the cache progress. You do need to call the following though dont you?

org.apache.log4j.BasicConfigurator.configure();

This is so frustrating, any other ideas?

S
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by sneaky thesnafu:
hmmm, I was excited when I read your post because I thought "oh of course" that autoDeploy defaults to true that's probably exactly the problem. Unfortunately it made no difference. As to your other points:

Have you checked, using the Manager App to see if "web-www" is still deployed from when you first ran it?


1)moving the context to an context.xml fragment in the conf/Catalina/localhost directory.
tried it, same results

I wasn't expecting that to change anything, just wanted to mention it.


2)I havent looked into the listeners, but you're probably right that its cleaner...not sure that it would solve my issue though.

Same as comment #1..


3)"startup" wasnt the right choice of words, all I was saying was that I was grabbing a logger reference in my startup class so I could log the cache progress. You do need to call the following though dont you?

org.apache.log4j.BasicConfigurator.configure();

I don't call configure. I just make sure the log4j.properties file is on the classpath (by putting it in my classes directory.).


This is so frustrating, any other ideas?

Yes, check with the manager app to see if you still have the other app deployed. If you undeploy with the manager app though, make sure you have a copy of your web-www tree backed up. The manager app will erase it.

Also, what version of Tomcat are you using?
[ January 16, 2005: Message edited by: Ben Souther ]
sneaky thesnafu
Greenhorn

Joined: Jan 16, 2005
Posts: 4
I'm using tomcat 5.0.28

To be honest with you I am completely unfamiliar with the manager app, I've always found those 'management' applications to be so flaky and I never trust them to do what I tell them to. Maybe this is unfair but I recall trying the manager app on an earlier versions of tomcat and it was awful so I never tried it again.

I tried to bring it up but got nothing from http://localhost:8080/manager.

This is probably because I've chopped up my server.xml to just deploy my one app in an effort to get rid of this problem. What do I need to do to get it working again? I'm willing to try anything.

I'll include my config files here:

server.xml

<?xml version='1.0' encoding='utf-8'?>
<Server>
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<GlobalNamingResources>
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
<Resource auth="Container" description="User database that can be updated and saved" name="UserDatabase" type="org.apache.catalina.UserDatabase"/>
<ResourceParams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</ResourceParams>
</GlobalNamingResources>
<Service name="Catalina">
<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8080" redirectPort="8443"/>
<Connector port="8009" protocol="AJP/1.3" protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8443"/>
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps" autoDeploy="false">
</Host>
</Engine>
</Service>
</Server>

context.xml (I've ommitted my database resource settings for obvious reasons

<Context path="" docBase="web-www" crossContext="false" debug="0" reloadable="false" >

<Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/>
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
</Context>
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Just see if you have a www-web dir under
TOMCAT_HOME/work/Catalina/localhost.
If so, delete it and restart Tomcat.
Also, if you still have that xml fragment under conf/Catalinal/localhost, get rid of that too, since you have a Context entry in server.xml.

That should take care of it.
sneaky thesnafu
Greenhorn

Joined: Jan 16, 2005
Posts: 4
yes I do have that web-www under the work directory, but if I delete it and then restart tomcat it just comes back.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Found it.

In 5.0, they called it deployOnStartup not autoDeploy
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/config/host.html

Set that to false, delete the web-www directory under work and try it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: server.xml quirk with startup servlet