How to implement the concurrent start of a servlet with the application?
sobes.tech AI
Answer from AI
The following mechanisms are used for simultaneously starting a servlet with the application:
-
The
<load-on-startup>element inweb.xml:- In the deployment descriptor
web.xml, for each servlet that should be loaded at application startup, the<load-on-startup>element is added. - The value of this element determines the load order of servlets. A positive integer indicates the load order (smaller numbers are loaded first). If the value is zero or negative, loading is "lazy" (upon the first request to the servlet).
- If multiple servlets have the same value, the load order is not guaranteed.
<!-- web.xml --> <web-app> <servlet> <servlet-name>MyStartupServlet</servlet-name> <servlet-class>com.example.MyStartupServlet</servlet-class> <load-on-startup>1</load-on-startup> <!-- Load at startup, first priority --> </servlet> <servlet> <servlet-name>AnotherStartupServlet</servlet-name> <servlet-class>com.example.AnotherStartupServlet</servlet-class> <load-on-startup>2</load-on-startup> <!-- Load at startup, after MyStartupServlet --> </servlet> <!-- ... other elements ... --> </web-app> - In the deployment descriptor
-
@WebServletannotation withloadOnStartupparameter:- In servlet containers supporting Servlet 3.0 and above, you can use the
@WebServletannotation directly above the servlet class. - The
loadOnStartupparameter in the annotation serves the same purpose as the<load-on-startup>element inweb.xml.
// com.example.MyStartupServlet.java import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(urlPatterns = "/startup", loadOnStartup = 1) // Load at startup with order 1 public class MyStartupServlet extends HttpServlet { @Override public void init(ServletConfig config) throws ServletException { super.init(config); // Initialization code executed at servlet startup System.out.println("MyStartupServlet initialized at application startup."); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Hello from MyStartupServlet!"); } }// com.example.AnotherStartupServlet.java import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(urlPatterns = "/another-startup", loadOnStartup = 2) // Load at startup with order 2 public class AnotherStartupServlet extends HttpServlet { @Override public void init(ServletConfig config) throws ServletException { super.init(config); // Initialization code System.out.println("AnotherStartupServlet initialized at application startup."); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Hello from AnotherStartupServlet!"); } } - In servlet containers supporting Servlet 3.0 and above, you can use the
When the application starts, the servlet container (e.g., Tomcat, Jetty) reads the deployment descriptor or scans annotations in servlet classes. For servlets with load-on-startup (or loadOnStartup) greater than or equal to zero, the container calls the init() method of the servlet immediately after loading and initializing the web application itself (Servlet Context). This guarantees that the servlet will be ready to operate before receiving the first request.
The main logic that should execute at application startup is implemented in the init() method of the servlet.