Tomcat Java Servlet - Initialize Class on Application Startup

后端 未结 3 1814
名媛妹妹
名媛妹妹 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:45

    If you want it to happen once for the whole app, and happen before any servlet is run, implement ServletContextListener and put your startup code in contextInitialized(). Then set up your web.xml to specify your class as a listener.

    Otherwise, you can do what the other answer says and put it in the init() method of the servlet.

提交回复
热议问题