JBoss AS 7: Servlet to the context root ("/") through code configuration

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) {
        // Do nothing.
    }
}

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?

!

+5
1

JBoss AS 7.1.1 Spring MVC 3.2.3.RELEASE. WebApplicationInitializer javadocs:

'/' Tomcat

Apache Tomcat DefaultServlet "/", Tomcat <= 7.0.14 . 7.0.15 . "/" GlassFish 3.1.

, web.xml, . , JBoss AS 7.1.1.Final JBoss Web 7.0.13, , , Tomcat 7.0.13 DefaultServlet, -, 7.0.15 .

, web.xml, . Bummers.

+6

All Articles