0

I had a Servlet that would connection to DB and load and store data into some collection objects for later use at my webapp startup. I defined this servlet as follows in my deployment descriptor.

web.xml

<servlet>
    <servlet-name>MasterDataLoader</servlet-name>
    <servlet-class>com.istore.web.controllers.MasterDataLoader</servlet-class>
    <load-on-startup>10</load-on-startup>
</servlet>

I am trying to replace the above usage with annotations in my Servlet hence modified my MasterDataLoader servlet as follows and removed the web.xml entry but this servlet never gets picked up by Tomcat 8 for some reason and hence data never gets loaded at sever startup. I do not see any errors in the error log. Am I missing anything ?

MasterDataLoader.java (With Annotations)

@WebServlet(name = "MasterDataLoader", loadOnStartup=10)
public class MasterDataLoader extends HttpServlet {

    private static final String DATASOURCE = "dataSource";

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        loadAll();
    }

    private void loadAll() {
        ServletContext appContext = getServletContext();
        appContext.setAttribute("menuCategories", getAllMenuCategories());
    }

    private List<Category> getAllMenuCategories() {
        LOG.debug("Fetching all menu categories from the db....");
        List<Category> categoriesLst = null;
        try {
            DataSource dataSource = (DataSource) getServletContext().getAttribute(DATASOURCE);
            categoriesLst = new MasterDao(dataSource).getAllCategories();
        } catch (SQLException sqle) {
            LOG.error("Exception while fetching all menu categories from the db....");
            LOG.error(sqle);
        }
        return categoriesLst;
    }

 ....
}
Nital
  • 5,784
  • 26
  • 103
  • 195

1 Answers1

0

Added urlPatterns to the annotation and it worked. Although the urlPatterns is not getting referenced/called anywhere in the code base. It looks like the urlPatterns is a mandatory attribute.

@WebServlet(name = "MasterDataLoader", urlPatterns = {"/dataLoader"}, loadOnStartup = 10)
Nital
  • 5,784
  • 26
  • 103
  • 195