使用当前代码,您需要执行GET请求以查看输出
HEREEEE。
如果您想在Servlet的启动时做一些事情(即
loadOnStartup,值大于或等于零的元素
0),则需要将代码放入init方法或Servlet的构造函数中:
@Overridepublic void init() throws ServletException { System.out.println("HEREEEE");}使用侦听器启动 应用程序范围 (中的
ServletContext)中的资源可能更方便。
import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;@WebListenerpublic class InitializeListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("On start web app"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("On shutdown web app"); }}例如,请参阅我的答案,以获取有关 _JAX-RS请求之间的共享变量_的问题。



