Search code examples
javaservletsinitializationstartupwebsphere-liberty

Load/initialize Java servlet on server startup


I have a Java servlet I would like to load & initialize on server startup. I assumed I could just add <load-on-startup>1</load-on-startup> to my <servlet> registration in my web.xml file but I find it still takes a "first request" to cause my app server (WebSphere Liberty) to actually load and initialize the servlet.

I have also tried registering a ServletContextListener in the same web.xml to no avail (it, too, is not called until that "first request").

Everything I read talks mostly about controlling the order of servlet loading/initialization, or, with regard to ServletContextListener, how you can be notified when your webapp is loaded. I find nothing that specifically talks about when the server starts up.

For your reference, here is my web.xml:

<web-app id="WebApp_ID" version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<servlet>
    <servlet-name>startup</servlet-name>
    <servlet-class>com.your.mother.StartupServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>startup</servlet-name>
    <url-pattern>/startup</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>com.your.mother.MyListener</listener-class>
</listener>

</web-app>

Solution

  • https://www.ibm.com/support/knowledgecenter/en/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ae/twlp_servlet_load.html

    <webContainer deferServletLoad="false"/>