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 classpath is an argument that tells the JVM where to find the classes and packages necessary to run a program. The classpath is always set from a source outside the program itself.
Tomcat is normally defined as a reference implementation of the Java Servlet and the Java Server Page (JSP) Specifications. It basically executes Java servlets and renders web pages which include JSP coding. It is available on the Apache site in both source and binary versions.
Tomcat itself is comprised of three main components: Jasper, Catalina, and Coyote. These components combined allow for the parsing and compilation of JavaServer Pages into java servlet code, the delivery of these servlets, and request processing.
CLASSPATH: CLASSPATH is an environment variable which is used by Application ClassLoader to locate and load the . class files. The CLASSPATH defines the path, to find third-party and user-defined classes that are not extensions or part of Java platform. Include all the directories which contain .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With