Summary: | ServletContext.getContext(String) always returns null for the root context ("/") | ||
---|---|---|---|
Product: | Tomcat 8 | Reporter: | Ivan Nikolchov <ivannikolchov> |
Component: | Catalina | Assignee: | Tomcat Developers Mailing List <dev> |
Status: | RESOLVED DUPLICATE | ||
Severity: | normal | CC: | ivannikolchov, tomasz.macnar |
Priority: | P2 | ||
Version: | 8.0.30 | ||
Target Milestone: | ---- | ||
Hardware: | PC | ||
OS: | All |
Description
Ivan Nikolchov
2015-03-21 18:03:59 UTC
*** This bug has been marked as a duplicate of bug 57645 *** Bug is still existing on tomcat 8.0.30 (and probably tomcat 7.x) I debug source code of tomcat and find, that ROOT context (with empty "path" in configuration server.xml) is added to ContainerBase class (HashMap name children) in key "", but i suppoust it shoud add ROOT context with key "/". Bug is probably in line 2036 in class org.apache.catalina.core.StandardContext, when set path. When i want to get root context (class ApplicationContext.getContext("/")) i can't call it with empty string parameter, because method check param and return null (!uri.startsWith("/")). When I call it with param "/" , I have null. Please check it - it is very important for us. The following JSP snippet works as expected: <% out.print("[" + request.getServletContext().getContext("/").getContextPath() + "]"); %> If you need assistance debugging why your application doesn't work then please use the users mailing list. *** This bug has been marked as a duplicate of bug 57645 *** Ok i agree, that this code work OK. But my case is other. I get ServletContext from servlet. Hire is my example: public class TestServlet extends HttpServlet { @Override public void init() throws ServletException { ServletContext servletContext = getServletContext(); System.out.println("Servlet START: " + servletContext.getContextPath()); String[] contextTab = { "/", "/app1", "/app2" }; for (String con : contextTab) { System.out.println("Context " + con + " : " + servletContext.getContext(con)); } } } And in context "/" always I have null. In server.xml i have configuration: <Context path="" docBase="${catalina.base}/webapps/ROOT" crossContext="true" /> <Context path="/app1" docBase="${catalina.base}/webapps/app1" crossContext="true" /> <Context path="/app2" docBase="${catalina.base}/webapps/app2" crossContext="true" /> And the output is: Servlet START: /app1 Context / : null Context /app1 : null Context /app2 : null Servlet START: /app2 Context / : null Context /app1 : org.apache.catalina.core.ApplicationContextFacade@8c6fb37 Context /app2 : null Servlet is added to app1 and app2. |