Basic method in a Java web application?

I am creating a web application with servlets, and I need the thread to change the number constantly and when the client makes a request, it gets the current number.

I tried to create a class with the main method and started the stream from there, and then from the servlet I get the instance where the stream works (is this possible?), But the application never enters the main method.

Any suggestions? Thanks

+4
source share
2 answers

Servlets run in a web container, and the main method of a web container goes out of your control.

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

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

+5
source

You must have a scheduled task running on your web server that updates this number. Web applications do not have a β€œcore” method (as at the application entry point), since each servlet is an independent entry point.

+1
source

Source: https://habr.com/ru/post/1438723/


All Articles