Quantcast

Problem with get ROOT context in servlet on tomcat 8.0.30 (jdk 1.7.0_79)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Problem with get ROOT context in servlet on tomcat 8.0.30 (jdk 1.7.0_79)

Tomasz Macnar
Hi,

I have problem with get ROOT servlet context from other application (on
the same server) in tomcat 8.0.30.
My code look this:

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));
         }
     }
}

This servlet I have in app1 and app2 with
<load-on-startup>1<load-on-startup>.

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


I debug source 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 "/".

Please tell me if i have wrong confugration or other thing ?

--
Pozdrawiam,
Tomasz Macnar

tel. +48 12 621 5234
mob. +48 694 464 934



smime.p7s (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with get ROOT context in servlet on tomcat 8.0.30 (jdk 1.7.0_79)

markt
On 31/12/2015 07:40, Tomasz Macnar wrote:

> Hi,
>
> I have problem with get ROOT servlet context from other application (on
> the same server) in tomcat 8.0.30.
> My code look this:
>
> 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));
>         }
>     }
> }
>
> This servlet I have in app1 and app2 with
> <load-on-startup>1<load-on-startup>.
>
> 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
>
>
> I debug source 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 suppose it should add ROOT context with
> key "/".

No.

> Please tell me if i have wrong configuration or other thing ?

The results above are giving you a big hint. app1 can't see any other
apps, but app2 can see app1 yet they are using exactly the same code.

Think about the order in which applications start and when the above
code runs.

Mark


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with get ROOT context in servlet on tomcat 8.0.30 (jdk 1.7.0_79)

Tomasz Macnar
Thanks for answer.
I understand that application deploy order is the key to get context to deployed applicaitons (when application isn't deployed i can't get it context - it is clear).
But ROOT application is deployed before app1, but i can't get ROOT context from app1. Is it in tomcat 8 possible to get ROOT context from other application (in servlet) ? If yes, coud tou give me an simple example ?

In tomcat 6 when i put this configuration to server.xml
<Context path="" docBase="${catalina.base}/webapps/ROOT" crossContext="true" />
i have returned ROOT application context. The same code work fine in tomcat 6.

Pozdrawiam,
Tomasz Macnar

tel. +48 12 621 5234
mob. +48 694 464 934
W dniu 2015-12-31 o 10:09, Mark Thomas pisze:
On 31/12/2015 07:40, Tomasz Macnar wrote:
Hi,

I have problem with get ROOT servlet context from other application (on
the same server) in tomcat 8.0.30.
My code look this:

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));
        }
    }
}

This servlet I have in app1 and app2 with
<load-on-startup>1<load-on-startup>.

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


I debug source 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 suppose it should add ROOT context with
key "/".
No.

Please tell me if i have wrong configuration or other thing ?
The results above are giving you a big hint. app1 can't see any other
apps, but app2 can see app1 yet they are using exactly the same code.

Think about the order in which applications start and when the above
code runs.

Mark


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]



smime.p7s (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with get ROOT context in servlet on tomcat 8.0.30 (jdk 1.7.0_79)

Konstantin Kolinko
2015-12-31 12:41 GMT+03:00 Tomasz Macnar <[hidden email]>:

> Thanks for answer.
> I understand that application deploy order is the key to get context to
> deployed applicaitons (when application isn't deployed i can't get it
> context - it is clear).
> But ROOT application is deployed before app1, but i can't get ROOT context
> from app1.
> Is it in tomcat 8 possible to get ROOT context from other
> application (in servlet) ? If yes, coud tou give me an simple example ?
>
> In tomcat 6 when i put this configuration to server.xml
>
> <Context path="" docBase="${catalina.base}/webapps/ROOT" crossContext="true"
> />
>
> i have returned ROOT application context. The same code work fine in tomcat
> 6.
>

Deployment order does not matter. What matters is whether other
context has already been started (registered in Mapper and is
available to serve requests). As you have already noted during your
debugging, "children" field in ContainerBase is a HashMap and the
entries are not ordered.

Note that
1) modern JRE uses random seed when evaluating String hash code to
mitigate hash collision attacks, so the order will be different on
different runs

2) In Tomcat 7 and later a container starts all its children in
parallel, using an executor configured with "startStopThreads"
property.,

3) The Servlet API getContext(path) is tricky as it uses prefix matching.
Asking for getContext("/app/foo") can return context for "/app/foo",
or context for "/app", or the ROOT context.
Discussed here:
http://markmail.org/thread/wwfgnc6qwgaenwol
https://bz.apache.org/bugzilla/show_bug.cgi?id=57645

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with get ROOT context in servlet on tomcat 8.0.30 (jdk 1.7.0_79)

Christopher Schultz-2
Konstantin,

On 12/31/15 12:56 PM, Konstantin Kolinko wrote:

> 2015-12-31 12:41 GMT+03:00 Tomasz Macnar <[hidden email]>:
>> Thanks for answer.
>> I understand that application deploy order is the key to get context to
>> deployed applicaitons (when application isn't deployed i can't get it
>> context - it is clear).
>> But ROOT application is deployed before app1, but i can't get ROOT context
>> from app1.
>> Is it in tomcat 8 possible to get ROOT context from other
>> application (in servlet) ? If yes, coud tou give me an simple example ?
>>
>> In tomcat 6 when i put this configuration to server.xml
>>
>> <Context path="" docBase="${catalina.base}/webapps/ROOT" crossContext="true"
>> />
>>
>> i have returned ROOT application context. The same code work fine in tomcat
>> 6.
>>
>
> Deployment order does not matter. What matters is whether other
> context has already been started (registered in Mapper and is
> available to serve requests). As you have already noted during your
> debugging, "children" field in ContainerBase is a HashMap and the
> entries are not ordered.
>
> Note that
> 1) modern JRE uses random seed when evaluating String hash code to
> mitigate hash collision attacks, so the order will be different on
> different runs

[citation needed]

Oracle's Java 8 does not do this, nor do I suspect any other JREs out
there, otherwise Serialization of things like HashMap would break all
over the place.

One could argue that modern JREs *should* do this, but backward
compatibility is an impediment.

-chris

> 2) In Tomcat 7 and later a container starts all its children in
> parallel, using an executor configured with "startStopThreads"
> property.,
>
> 3) The Servlet API getContext(path) is tricky as it uses prefix matching.
> Asking for getContext("/app/foo") can return context for "/app/foo",
> or context for "/app", or the ROOT context.
> Discussed here:
> http://markmail.org/thread/wwfgnc6qwgaenwol
> https://bz.apache.org/bugzilla/show_bug.cgi?id=57645
>
> Best regards,
> Konstantin Kolinko
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with get ROOT context in servlet on tomcat 8.0.30 (jdk 1.7.0_79)

Konstantin Kolinko
2016-01-04 2:22 GMT+03:00 Christopher Schultz <[hidden email]>:

> Konstantin,
>
> On 12/31/15 12:56 PM, Konstantin Kolinko wrote:
>> 2015-12-31 12:41 GMT+03:00 Tomasz Macnar <[hidden email]>:
>>> Thanks for answer.
>>> I understand that application deploy order is the key to get context to
>>> deployed applicaitons (when application isn't deployed i can't get it
>>> context - it is clear).
>>> But ROOT application is deployed before app1, but i can't get ROOT context
>>> from app1.
>>> Is it in tomcat 8 possible to get ROOT context from other
>>> application (in servlet) ? If yes, coud tou give me an simple example ?
>>>
>>> In tomcat 6 when i put this configuration to server.xml
>>>
>>> <Context path="" docBase="${catalina.base}/webapps/ROOT" crossContext="true"
>>> />
>>>
>>> i have returned ROOT application context. The same code work fine in tomcat
>>> 6.
>>>
>>
>> Deployment order does not matter. What matters is whether other
>> context has already been started (registered in Mapper and is
>> available to serve requests). As you have already noted during your
>> debugging, "children" field in ContainerBase is a HashMap and the
>> entries are not ordered.
>>
>> Note that
>> 1) modern JRE uses random seed when evaluating String hash code to
>> mitigate hash collision attacks, so the order will be different on
>> different runs
>
> [citation needed]
>
> Oracle's Java 8 does not do this, nor do I suspect any other JREs out
> there, otherwise Serialization of things like HashMap would break all
> over the place.
>
> One could argue that modern JREs *should* do this, but backward
> compatibility is an impediment.

Apparently this one:
http://hg.openjdk.java.net/jdk9/dev/jdk/rev/43bd5ee0205e
May 2012, so 3,5 years ago. Times flies fast..

String.java in JDK 7u80 has that code (HASHING_SEED field, hash32()
method etc.).

Implementation of String.hashCode() is immutable, because its
algorithm is documented in official Javadoc for the method, but they
went with a package-visible method "hash32()". The hg commit creates
"hash32()" as a public method. It is package-visible in actual 7u80.

There is no such method in 8u66, but the structure of a hashmap has
evolved. There is no guarantee of an iteration order.

Implementation of hashCode() does not matter for serialization,
because HashMap serializes only the values (the key-value pairs), not
the internal structure.


Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [OT] Problem with get ROOT context in servlet on tomcat 8.0.30 (jdk 1.7.0_79)

Christopher Schultz-2
Konstantin,

On 1/3/16 8:07 PM, Konstantin Kolinko wrote:

> 2016-01-04 2:22 GMT+03:00 Christopher Schultz <[hidden email]>:
>> Konstantin,
>>
>> On 12/31/15 12:56 PM, Konstantin Kolinko wrote:
>>> 2015-12-31 12:41 GMT+03:00 Tomasz Macnar <[hidden email]>:
>>>> Thanks for answer.
>>>> I understand that application deploy order is the key to get context to
>>>> deployed applicaitons (when application isn't deployed i can't get it
>>>> context - it is clear).
>>>> But ROOT application is deployed before app1, but i can't get ROOT context
>>>> from app1.
>>>> Is it in tomcat 8 possible to get ROOT context from other
>>>> application (in servlet) ? If yes, coud tou give me an simple example ?
>>>>
>>>> In tomcat 6 when i put this configuration to server.xml
>>>>
>>>> <Context path="" docBase="${catalina.base}/webapps/ROOT" crossContext="true"
>>>> />
>>>>
>>>> i have returned ROOT application context. The same code work fine in tomcat
>>>> 6.
>>>>
>>>
>>> Deployment order does not matter. What matters is whether other
>>> context has already been started (registered in Mapper and is
>>> available to serve requests). As you have already noted during your
>>> debugging, "children" field in ContainerBase is a HashMap and the
>>> entries are not ordered.
>>>
>>> Note that
>>> 1) modern JRE uses random seed when evaluating String hash code to
>>> mitigate hash collision attacks, so the order will be different on
>>> different runs
>>
>> [citation needed]
>>
>> Oracle's Java 8 does not do this, nor do I suspect any other JREs out
>> there, otherwise Serialization of things like HashMap would break all
>> over the place.
>>
>> One could argue that modern JREs *should* do this, but backward
>> compatibility is an impediment.
>
> Apparently this one:
> http://hg.openjdk.java.net/jdk9/dev/jdk/rev/43bd5ee0205e
> May 2012, so 3,5 years ago. Times flies fast..
>
> String.java in JDK 7u80 has that code (HASHING_SEED field, hash32()
> method etc.).
>
> Implementation of String.hashCode() is immutable, because its
> algorithm is documented in official Javadoc for the method, but they
> went with a package-visible method "hash32()". The hg commit creates
> "hash32()" as a public method. It is package-visible in actual 7u80.

In my Oracle JDK 1.8.0_60 (x86-64 Mac) src.zip, java/lang/String.java
has no such field, etc.

A little research shows that JDK 1.7.0u8 and higher had this seed and
modified hashing, but that Java 8 threw out all that stuff and just
started using tree-based maps with no fudge-factor on the String hash.
That makes for a cleaner separation between the String and HashMap (and
related) classes.

That's why I can't find it in a recent JDK release.

> There is no such method in 8u66, but the structure of a hashmap has
> evolved. There is no guarantee of an iteration order.

There was never a guarantee of an iteration order from HashMap.

> Implementation of hashCode() does not matter for serialization,
> because HashMap serializes only the values (the key-value pairs), not
> the internal structure.

I hadn't read HashMap.writeObject; thanks for pointing that out. If each
HashMap decided to mutate the hashes of its String keys, they would in
fact still be compatible with JREs that did not perform such mutations.

-chris

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with get ROOT context in servlet on tomcat 8.0.30 (jdk 1.7.0_79)

mwbxtr
In reply to this post by markt
I believe I am having an issue with getting the ROOT context from a different app context. This was working in 8.0.14, but no longer works in 8.0.43.

Root context exists and (includes crossContext=true in conf/context.xml)

Within Context: /app1    

public class CrossContextExample {

    private ServletContext app1Context;

    public someMethod() {
        ServletContext rootContext = this.app1Context.getContext("/");
        // rootContext == null :( Used to be the ServletContext of ROOT
    }
}

I think this came as a result of this change by markt?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with get ROOT context in servlet on tomcat 8.0.30 (jdk 1.7.0_79)

markt
On 15/05/2017 14:49, mwbxtr wrote:
> I believe I am having an issue with getting the ROOT context from a different
> app context. This was working in 8.0.14, but no longer works in 8.0.43.

This works for me with a clean build of 8.0.x.

You'll need to put together the simplest test case that demsontrates the
problem starting from a clean install of the latest 8.0.x release.

Mark


>
> Root context exists and (includes crossContext=true in conf/context.xml)
>
> Within Context: /app1
>
> public class CrossContextExample {
>
>      private ServletContext app1Context;
>
>      public someMethod() {
>          ServletContext rootContext = this.app1Context.getContext("/");
>          // rootContext == null :( Used to be the ServletContext of ROOT
>      }
> }
>
> I think this came as a result of  this change
> <https://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/java/org/apache/catalina/core/ApplicationContext.java?r1=1636341&r2=1642698>
> by markt?
>
>
>
> --
> View this message in context: http://tomcat.10.x6.nabble.com/Problem-with-get-ROOT-context-in-servlet-on-tomcat-8-0-30-jdk-1-7-0-79-tp5044449p5063440.html
> Sent from the Tomcat - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...