Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

main class of a tomcat web application

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?

like image 703
TheFrenchGuy Avatar asked Dec 25 '11 16:12

TheFrenchGuy


People also ask

What is class path in Tomcat?

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.

What is a Tomcat web application?

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.

What are the three major components of Tomcat?

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.

What is classpath in web application?

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 .


1 Answers

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.

like image 96
Óscar López Avatar answered Oct 05 '22 09:10

Óscar López