1

Note: this is not cross-posting, asking different questions (related ClassCastError when share objects between webapp)

I have two webapps running at two different contexts on the same tomcat instance, when the 2nd apps tried to get the first app context, regardless the name I input, the object is the same context, which is wrong.

public void contextInitialized(ServletContextEvent sce) {
       ServletContext context= sce.getServletContext().getContext("/bling");
       ServletContext context2= sce.getServletContext().getContext("/bla");

Both objects context and context2 have this object id org.apache.catalina.core.ApplicationContextFacade@4376.

I'm using tomcat7. Any tips? tks

Update: Tks for all good suggestions. The problem just happens if the 2nd webapps (where the above snippet is) is deployed at ROOT of the server (a must in my structure, cannot change). Any more hints. Tks.

4
  • have a look here javapapers.com/servlet/… Commented Mar 28, 2013 at 5:18
  • @LuiggiMendoza you might be wrong, the manual says this: The given path must be begin with "/", is interpreted relative to the server's document root and is matched against the context roots of other web applications hosted on this container. Commented Mar 28, 2013 at 5:18
  • Related: stackoverflow.com/q/2088633/1065197 Commented Mar 28, 2013 at 5:21
  • @panky1986 your link is not related with the question at all. Commented Mar 28, 2013 at 5:21

1 Answer 1

2

I'm guessing it's a timing issue. Maybe when ServletContext A is initialized, and trying to obtain ServletContext B, ServletContext B is not yet initialized / deployed.

To prove this, try another test inside a servlet doGet, and invoke it when you're sure both webapps are deployed properly

Sign up to request clarification or add additional context in comments.

4 Comments

I thought the ServletContext was created when deploying the apps, and in Tomcat this happens when starting the server.
But the OP's code seem to be running on context initialized event, maybe it's too early to do this
Hi, tks for your answers. I've updated my question, appreciate if you can provide more answers.
Any news on this ? I think @gerrytan was correct with his suspicion that it was indeed a timing issue. Can you confirm (and then accept his answer) Taxi ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.