Tomcat Java Servlet - Initialize Class on Application Startup

后端 未结 3 1798
名媛妹妹
名媛妹妹 2020-12-30 09:01

I have a class that takes a bit of time to start up (makes some JNI calls and what not), so it is not feasable to initialize this class everytime a page loads. Is it possibl

3条回答
  •  醉梦人生
    2020-12-30 09:51

    You have two choices:

    1. Initialize your class in servlet's init() method. You may add attribute to make sure your servlet is created at application startup and not on first access.

    2. Add ServletContextListener and use contextInitialized() callback method. Use ServletContext#setAttribute to store created object for future use.

提交回复
热议问题