[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 851
  • Last Modified:

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 ?
0
dhuma
Asked:
dhuma
  • 5
  • 4
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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!
0
 
dhumaAuthor Commented:
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.
0
 
dhumaAuthor Commented:
any solutions.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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?
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
0
 
dhumaAuthor Commented:
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.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Ah, ok, I was assuming that you were loading the context via web.xml. Is there a particular need that you have that you are loading the context via Java code? If not then there is another way, that is the usual way to bootstrap a Spring application context in web applications. Add the following lines to your web.xml file...

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application-context.xml
		</param-value>
	</context-param>
	
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

Open in new window

Once you do this, then this context will be the root/parent of the servlet contexts (if you are using Spring's DispatcherServlet) or available via    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());   in your servlets (if you are coding your servlet as a directly subclass of HttpServlet), as is done in the link I posted above.
0
 
dhumaAuthor Commented:
thanks for your response.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Not a problem, glad to help!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now