4

I'm creating a web application with servlets and I need a thread changing a number constantly and when a client do a request it gets the current number.

I tried creating a class with the main method and from there start the thread, then from the servlet get the instance where the thread is running (can this be possible?), but the application never enters to the main method.

Any suggestions? Thanks

puma91
  • 336
  • 6
  • 15

2 Answers2

6

Servlets run in a web container and the web container's main method is out of your control.

If you want to perform any startup operations, then the servlet framework provides context listeners that can be registered with the framework. These listeners are called when your web application starts.

Alternatively, if you want to perform some operation on each incoming request or outgoing response, then you can use servlet filters

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Vikdor
  • 23,934
  • 10
  • 61
  • 84
1

You should have a scheduled task that runs on your webserver, that updates that number. There is no "main" method (as in application entry point) in web applications, since each servlet is an independent entry point.

jprusakova
  • 1,557
  • 3
  • 19
  • 31