Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How to run a class that loops forever on Tomcat startup?

Avatar of killdurst
killdurst asked on
JavaApache Web ServerJava EE
2 Comments1 Solution981 ViewsLast Modified:
Hi, I would like to run a program that loops forever, in the background, when Tomcat starts. I created MyServlet.java, and in its init() method, I included the following code...

    System.out.println("Hello world!");
    Thread t = new Thread();

So basically the code will output "Hello world!" to the console every minute.

In my web.xml, I included the following tag...


So my Tomcat will execute MyServlet when it starts.

The problem is that I can't access the web application at all since MyServlet is still running. Since it will run continuously until the Tomcat shuts down, that means I can't access the web application at all. How do I run a program, that loops continuously, in the background, whenever Tomcat starts, and which will not affect the other servlets in my web application? Thanks!
Avatar of dpearson

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answers