You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Sunny L.S.Chan" <su...@delirium.com.hk> on 2000/12/23 10:05:31 UTC
Tomcat 3.2.1 servlet load on startup problem
Hi, I have been following the instructions in the tomcat faq to start a
servlet whenever tomcat starts, but with no luck, can someone give me a
light?
When-ever I start tomcat, the console should beam out a message which says
"Servlet Loaded" and set a "appServerPath" variable, however, its just
simply not load.
I can run the servlet manually with no problem, the console will beam out
the message, and set that variable
here is the context path in the server.xml of tomcat:
<Context path="/myDir"
docBase="C:\myDir"
crossContext="true"
debug="0"
reloadable="true"
trusted="false" >
</Context>
Here is the web.xml in the C:\myDir\WEB-INF\:
which dezscribes the servlet to load:
<servlet>
<servlet-name>servletInit_newsgroup</servlet-name>
<servlet-class>com.myservlet.servletInit_newsgroup</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
the servlet is located in C:\myDir\WEB-INF\classes\com\myservlet\
am I missing something?
Thanks!
Re: Tomcat 3.2.1 servlet load on startup problem
Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
"Sunny L.S.Chan" wrote:
> Hi, I have been following the instructions in the tomcat faq to start a
> servlet whenever tomcat starts, but with no luck, can someone give me a
> light?
> When-ever I start tomcat, the console should beam out a message which says
> "Servlet Loaded" and set a "appServerPath" variable, however, its just
> simply not load.
> I can run the servlet manually with no problem, the console will beam out
> the message, and set that variable
>
> here is the context path in the server.xml of tomcat:
> <Context path="/myDir"
> docBase="C:\myDir"
> crossContext="true"
> debug="0"
> reloadable="true"
> trusted="false" >
> </Context>
>
> Here is the web.xml in the C:\myDir\WEB-INF\:
> which dezscribes the servlet to load:
> <servlet>
> <servlet-name>servletInit_newsgroup</servlet-name>
> <servlet-class>com.myservlet.servletInit_newsgroup</servlet-class>
> <load-on-startup>1</load-on-startup>
> </servlet>
>
> the servlet is located in C:\myDir\WEB-INF\classes\com\myservlet\
>
> am I missing something?
>
You've got all the configuration stuff right ... the problem is in your servlet.
Your initialization message is generated in the doGet() method, which is only called
when a request actually comes in. If you want to do things when the servlet is first
loaded, place that code in the init() method instead.
Note that init() does not receive a request object (because it is not called as the
result of a request). Therefore, you will need to use some other technique to
initialize your "appServerPath" context attribute. A common technique would be to use
a servlet initialization parameter that is read in the init method:
<servlet>
<servlet-name>servletInit_newsgroup</servlet-name>
<servlet-class>com.myservlet.servletInit_newsgroup</servlet-class>
<init-param>
<param-name>serverPath</param-name>
<param-value>http://www.mycompany.com:8080</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
and then in your servlet:
public void init() throws ServletException() {
String path = getServletConfig().getInitParameter("serverPath");
getServletContext().setAttribute("appServerPath", path);
System.out.println("Servlet loaded");
}
>
> Thanks!
>
Craig McClanahan