简体   繁体   English

tomcat Web应用程序的主类

[英]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. 服务器由带有jersey的restful服务组成,并部署在tomcat 7上。实际上,我需要在客户端访问服务之前创建服务的上下文(读取一些高大小的文件)。 Is it possible to create a main class of my webapp or not? 是否可以创建我的webapp的主类?

A web application in JavaEE doesn't have a "main class" in the same sense that a desktop application does; JavaEE中的Web应用程序没有与桌面应用程序相同的“主类”; 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. 当然,执行必须从某个main方法开始执行,但它将由Web容器(在您的情况下为Tomcat)管理,并且不在您的范围之内。

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). 你可以做的是创建一个servlet,它使用init方法在应用程序上下文中预加载你需要的数据(假设所有客户端的数据都相同,理想情况下,它们不会被它们修改) 。 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. 此外,在servlet配置中,您指定必须在启动时加载servlet,这样您就可以确保在应用程序开始时加载一次数据,并确保所有客户端都能够访问它从应用程序上下文。

EDIT : 编辑:

In more recent versions of the Servlet specification (2.3+) the preferred way is to use context listeners, see this answer for details. 在最新版本的Servlet规范(2.3+)中,首选方法是使用上下文侦听器,有关详细信息,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM