Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load On Start Up using Annotation in JAVA

I have this code,

@WebServlet(value="/initializeResources", loadOnStartup=1)
public class InitializeResources extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      System.out.println("HEREEEE");
  }

}

But the servlet doesn't start when the web application is started.

How use load on startup on Servlet Annotation?

My Servlet API is 3.0 and I use Tomcat 7

like image 334
Diego Alves Avatar asked Sep 15 '13 04:09

Diego Alves


People also ask

What is use of load-on-startup?

The element 'load-on-startup' is used to load the servlet. The 'void init()' method of servlet gets executed when the server gets started. The element content of 'load-on-startup' is Integer. if the integer is negative: The container loads servlet at any time.

What is the use of @WebServlet annotation?

Use the @WebServlet annotation to define a servlet component in a web application. This annotation is specified on a class and contains metadata about the servlet being declared. The annotated servlet must specify at least one URL pattern. This is done by using the urlPatterns or value attribute on the annotation.

How are servlets loaded on startup?

The <load-on-startup> element indicates that the servlet should be loaded on the startup of the web application. The optional contents of this element must be a positive integer that specifies the order in which the servlet should be loaded. Servlets with lower values are loaded before servlets with higher values.

What's a WebServlet?

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.


1 Answers

With you current code, you need to do a GET request for see the output HEREEEE.

If you want to do something on the startup of the servlet (i.e. the element loadOnStartup with value greater or equal to zero, 0), you need put the code in a init method or in the constructor of the servlet:

@Override
public void init() throws ServletException {
    System.out.println("HEREEEE");
}

It may be more convenient to use a listener to start a resource in the application scope (in the ServletContext).

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class InitializeListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("On start web app");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("On shutdown web app");
    }

}

For an example, see my answer for the question Share variables between JAX-RS requests.

like image 160
Paul Vargas Avatar answered Sep 28 '22 20:09

Paul Vargas