"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:
> docBase="C:\myDir"
> crossContext="true"
> debug="0"
> reloadable="true"
> trusted="false" >
>
>
> Here is the web.xml in the C:\myDir\WEB-INF\:
> which dezscribes the servlet to load:
>
> servletInit_newsgroup
> com.myservlet.servletInit_newsgroup
> 1
>
>
> 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:
servletInit_newsgroup
com.myservlet.servletInit_newsgroup
serverPath
http://www.mycompany.com:8080
1
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