load-on-startup problem tomcat
I want to load a class on tomcat startup which will actually initialize variables in other classes.
i have edited the appName/WEB-INF/web.xml as follows
<servlet>
<servlet-name>LoadConfigurations</servlet-name>
<servlet-class>Lo开发者_开发问答ader.LoadConfigurations</servlet-class>
<init-param>
<param-name>env</param-name>
<param-value>dev</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
and i have placed my servlet LoadConfiguration in /appName/WEB-INF/classes
how do i check whether the servlet is beig called or not ? because when i try to display the value from initialized class it returns null
There are two problems:
The compiled class needs to go into a folder structure base on the package name. In your case a folder called
Loader
In order to call the servlet, you need to add a
<ServletMapping>
to yourweb.xml
file
The container should call your servlets init() method when the servlet is initialized. Implement/override that method and try writing something to the log from there.
This is how I fix it on tomcat 9:
Edit the
conf/context.xml
file and addreloadable="false"
to the<Context>
tag.<Context reloadable="false">
Edit the
conf/server.xml
file and adddeployOnStartup="false"
to the<Host>
tag.<Host appBase="webapps" deployOnStartup="false" …>
This is the solution for Tomcat 7.0
Step 1: Create war file for your webapp/servlets. If you are using Eclipse, File->Export->Web->WAR file, and save it to a known location.
Step 2: Find out the home folder for your tomcat. For that, go to tomcat/apache-tomcat-7.0.41/bin and execute ./startup.sh This will print out couple of global variable names. Note down the one for CATALINA_HOME.
Step 3: Copy the war file from Step 1 in CATALINA_HOME/webapps
Step 4: Next, Create an xml file in CATALINA_HOME/conf/{Engine}/localhost/MyServlets.xml :
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<Context deployOnStartup="true" docBase="/home/ubuntu/Downloads/apache-tomcat-7.0.42/webapps/" reloadable="true">
<Manager pathname=""/>
</Context>
Change docBase to point to location where you copied the war file in Step 3.
Now, you can go go to tomcat/apache-tomcat-7.0.41/bin and execute ./startup.sh. Your servlets will be automatically started. Hope this helps.
精彩评论