Bug 57645 - getContext("/") returns null
Summary: getContext("/") returns null
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 7
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 7.0.59
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
: 57733 (view as bug list)
Depends on:
Blocks:
 
Reported: 2015-02-27 21:27 UTC by Karl Olson
Modified: 2015-12-31 17:50 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
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