Using JBoss AS 7, I am trying to configure a Servlet 3.0 container using Java code instead of web.xml. My problem is that when I register a servlet mapped to the context root ("/"), the servlet defaults to priority and processes requests. I have not tried working with ServletContextListener and ServletContainerInitializer.
Attempt 1: ServletContextListener
@WebListener
public class AppInitializer implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
ServletRegistration.Dynamic homeServlet = context.addServlet("homeServlet", new HomeServlet());
homeServlet.addMapping("/");
homeServlet.setLoadOnStartup(1);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
}
Attempt 2: ServletContainerInitializer
public class AppInitializer2 implements ServletContainerInitializer {
@Override
public void onStartup(Set<Class<?>> classes, ServletContext context) throws ServletException {
ServletRegistration.Dynamic homeServlet = context.addServlet("homeServlet", new HomeServlet());
homeServlet.addMapping("/");
homeServlet.setLoadOnStartup(1);
}
}
Additional Information
- If I change the display from
/to /example, my servlet correctly processes requests for a new path. - If I register my servlet
/via web.xml instead of Java code, my servlet correctly processes requests to the context root.
... , Java DefaultServlet?
!