Bug 57645

Summary: getContext("/") returns null
Product: Tomcat 7 Reporter: Karl Olson <karl.olson>
Component: CatalinaAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal CC: ivannikolchov
Priority: P2    
Version: 7.0.59   
Target Milestone: ---   
Hardware: All   
OS: All   
Attachments: test.war

Description Karl Olson 2015-02-27 21:27:38 UTC
The getContext() method incorrectly returns null when requesting the ROOT context ("/"). The following fix resolves the issue:

    public ServletContext getContext(String uri) {

        // Validate the format of the specified argument
        if ((uri == null) || (!uri.startsWith("/")))
            return (null);

        Context child = null;
        try {
            // Look for an exact match
            Container host = context.getParent();
            child = (Context) host.findChild(uri);
            if ( (child == null) && "/".equals(uri) ) { // fix
              child = (Context) host.findChild("");     // fix
            }                                           // fix

The root context was created via:

  Context context = embedded.addWebapp("", "/path/to/webapps/ROOT");
Comment 1 Christopher Schultz 2015-02-27 22:43:48 UTC
I believe this was reported recently as a regression due to the fix for bug #57190 and fixed in r1661867 (trunk) r1662178 (Tomcat 8) and r1662179 (Tomcat 7).

There was no previous BZ issue for it, so this one is as good as any to document it.

Changelog for the Tomcat 7 fix:

  <fix>
    Correct a regression in the fix for <bug>57190</bug> that incorrectly
    required the path passed to
    <code>ServletContext.getContext(String)</code> to be an exact match to a
    path to an existing context. (markt)
  </fix>

This fix will be included in Tomcat 7.0.60 and later.
Comment 2 Christopher Schultz 2015-03-21 22:06:06 UTC
*** Bug 57733 has been marked as a duplicate of this bug. ***
Comment 3 Mark Thomas 2015-12-29 17:25:35 UTC
*** Bug 57733 has been marked as a duplicate of this bug. ***
Comment 4 Mark Thomas 2015-12-30 19:32:14 UTC
*** Bug 57733 has been marked as a duplicate of this bug. ***
Comment 5 Konstantin Kolinko 2015-12-31 17:50:33 UTC
(In reply to Christopher Schultz from comment #1)
> I believe this was reported recently as a regression due to the fix for bug
> #57190 and fixed in r1661867 (trunk) r1662178 (Tomcat 8) and r1662179
> (Tomcat 7).
> 
> There was no previous BZ issue for it, so this one is as good as any to
> document it.
> 

For a reference,
discussion about ServletContext.getContext() API on dev@ list of that time,

Thread "Two serious issues have been introduced in Tomcat 7 and Tomcat 8..."
started 2015-02-23,
http://markmail.org/thread/wwfgnc6qwgaenwol
Comment 6 Paul Taylor 2017-07-26 13:14:02 UTC
It looks like this bug was re-introduced on the Tomcat 7 branch in 7.0.61
Comment 7 Konstantin Kolinko 2017-07-26 13:44:48 UTC
(In reply to Paul Taylor from comment #6)

Quick test: It does work in 7.0.78.

Note that the ServletContext.getContext() method is disabled by default for security reasons. It must be explicitly enabled by setting crossContext="true" in Context configuration of the calling web application.
Comment 8 Konstantin Kolinko 2017-07-26 13:48:23 UTC
Created attachment 35180 [details]
test.war

Sample web application that demonstrates that the feature is working successfully.

Steps:
1. Deploy it as webapps/test.war
2. Start Tomcat
3. Access http://localhost:8080/test/
It prints:
getContext("/"): org.apache.catalina.core.ApplicationContextFacade@16b6c55

Tested with 7.0.78.