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 ?
dhumaAsked:
Who is Participating?
 
mccarlConnect With a Mentor IT 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
 
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
dhumaAuthor Commented:
any solutions.
0
 
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
 
dhumaAuthor Commented:
thanks for your response.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Not a problem, glad to help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.