Bug 53863 - Unable to override default servlet when using Tomcat in embedded mode
Unable to override default servlet when using Tomcat in embedded mode
Status: RESOLVED FIXED
Product: Tomcat 7
Classification: Unclassified
Component: Catalina
7.0.30
PC All
: P2 critical (vote)
: ---
Assigned To: Tomcat Developers Mailing List
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2012-09-13 03:44 UTC by Fernando Ribeiro
Modified: 2012-09-17 01:12 UTC (History)
1 user (show)



Attachments
testcase (11.30 KB, application/octet-stream)
2012-09-13 03:45 UTC, Fernando Ribeiro
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Fernando Ribeiro 2012-09-13 03:44:28 UTC
An implementation of ServletContainerInitializer can't add a mapping to "/" because the default servlet is loaded first, as first reported in 51278:

public final class SampleServletContainerInitializer implements ServletContainerInitializer {

  public void onStartup(final Set<Class<?>> c, final ServletContext ctx) throws ServletException {
    final XmlWebApplicationContext appCtx = new XmlWebApplicationContext();

    appCtx.setConfigLocation("/WEB-INF/DispatcherServlet-servlet.xml");

    final ServletRegistration.Dynamic dispatcher = ctx.addServlet("DispatcherServlet", new DispatcherServlet(appCtx));

    dispatcher.setLoadOnStartup(1);

    final Set<String> conflictSet = dispatcher.addMapping("/");

    for (final String conflict : conflictSet)
      out.println(conflict); // prints "/"

  }

}

A test case follows attached.
Comment 1 Fernando Ribeiro 2012-09-13 03:45:05 UTC
Created attachment 29366 [details]
testcase
Comment 2 Mark Thomas 2012-09-16 18:19:35 UTC
There is a bug here but it was hard work to find. The lack of key information provided with the test case did not help. For future reference, the following information would have saved some considerable time:
- Maven version required (build failed with 2.2.1, worked with 3.0.4)
- Steps to execute the test case
- Make clear the issue was with embedded mode

The best test cases are as small as possible and are written as unit tests (of which plenty of examples exist in the code base - including ones that use SCIs).

The bug was that the implicit servlets (JSP, default) when added in embedded mode were not marked as override-able. This has been fixed in trunk and 7.0.x and will be included in 7.0.31 onwards.
Comment 3 Fernando Ribeiro 2012-09-17 01:12:31 UTC
I will make sure future tickets provide better info. Thanks.