I have a client server application. The server is made of restful services with jersey and is deployed on tomcat 7. Actually, I need to create the context of the services (read some high sized files) before the client access to the services. Is it possible to create a main class of my webapp or not?
A web application in JavaEE doesn't have a "main class" in the same sense that a desktop application does; surely, the execution must start on a main
method somewhere, but it'll be managed by the web container (Tomcat in your case) and outside of your reach.
What you can do instead, is create a servlet which preloads the data you need in the application context using its init
method (assuming that the data will be the same for all the clients, and ideally, it won't be modified by them). Also, in the servlet configuration, you specify that the servlet must be loaded on startup, and in that way you make sure that the data will be loaded once at the beginning of the application, and that all the clients will be able to access it from the application context.
EDIT :
In more recent versions of the Servlet specification (2.3+) the preferred way is to use context listeners, see this answer for details.
© 2022 - 2024 — McMap. All rights reserved.