Link to home
Start Free TrialLog in
Avatar of dhuma
dhuma

asked on

sharing application context in web application

I am loading the spring application context in one Servlet, also need the reference to the context in another servlet. How can I achieve this ?
Avatar of mccarl
mccarl
Flag of Australia image

All servlets defined within a webapp should have access to the root application context defined in that webapp.

So I am guessing that what you wrote in your question might not be exactly what you meant? Did you mean that you need to access the app context of one webapp from a different webapp? (ie. a different WAR file) If this is the case then I would ask, can you provide more detail about what you are trying to do because you either A) don't really need to or B) shouldn't really be doing it that way!
Avatar of dhuma
dhuma

ASKER

All in the same webapp.

I have two servlets in a given webapp.

Both of them need the beans specified in the Spring application context. How can I share the context between the Servlets with in the same webapp.
Avatar of dhuma

ASKER

any solutions.
Ok, well as I said both servlets have access to the root application context, since it is the parent context to each servlet context. So if you need to access beans from the XXXX-servlet.xml you can just refer to them directly. If you need to access them from code (and you can't inject them via the servlet context) you should be able to access the context via the HttpServletRequest object or WebApplicationUtils class. I can check exact syntax later when I am at a PC.

If this still isn't what you are after, then I missing something, so could you post some code/XML and further explain what you are trying to do?
Check out this, http://mythinkpond.wordpress.com/2010/03/22/spring-application-context/, for a number of ways to achieve what you are after, depending on how you are using Spring. Also note that in either of the methods specified, nothing is preventing you from having multiple servlet's defined in the web.xml and any/all of them can access the Spring app context in the same way.
Avatar of dhuma

ASKER

I am talking about this Spring appplication context

ApplicationContext ctx = new ClassPathXmlApplicationContext(
                        "application-context.xml");


How do I share this between Servlets in a given webapp. Want to avoid loading the "application-context.xml" in two different servlets.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dhuma

ASKER

thanks for your response.
Not a problem, glad to help!