Search code examples
javaspring-mvcjakarta-ee

How can I Load a class while startup tomcat Server


I would like to load java class file that contain db related function. How can I load that java file while starting the tomcat server


Solution

  • you can use servlet for that as below, define into web.xml

    <servlet>
       <servlet-name>YourServletName</servlet-name>
       <servlet-class>com.abc.xyz.YourServletClassName</servlet-class>
       <load-on-startup>1</load-on-startup>
    </servlet>
    

    in YourServletClassName.java file you can write your code.

    Hope it helps you.