tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
Subject Re: Tomcat 3.2.1 servlet load on startup problem
Date Sat, 23 Dec 2000 16:48:57 GMT
"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



Mime
View raw message