blob: a071baae81cb92ecc8fdfeb9821d1e645bb63b01 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>4.5&nbsp;Programmatic Access to the Servlet Context</title><link rel="stylesheet" href="css/stylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"><link rel="home" href="index.html" title="Virgo Programmer Guide"><link rel="up" href="ch04.html" title="4.&nbsp;Developing Applications"><link rel="prev" href="ch04s04.html" title="4.4&nbsp;Creating and Using Configuration Artifacts"><link rel="next" href="ch04s06.html" title="4.6&nbsp;Web Application Manifest Processing"><!--Begin Google Analytics code--><script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
var pageTracker = _gat._getTracker("UA-2728886-3");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
</script><!--End Google Analytics code--></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">4.5&nbsp;Programmatic Access to the Servlet Context</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch04s04.html">Prev</a>&nbsp;</td><th width="60%" align="center">4.&nbsp;Developing Applications</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ch04s06.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="developing-applications-programmatic-access"></a>4.5&nbsp;Programmatic Access to the Servlet Context</h2></div></div></div><p>
This section describes how to programmatically access the servlet context to obtain the WebApplicationContext or the BundleContext.
</p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="developing-applications-programmatic-access-web"></a>Programmatic Access to Web Features</h3></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="developing-applications-programmatic-access-web-application-context"></a>Programmatic Access to the WebApplicationContext</h4></div></div></div><p>
The Virgo Tomcat Server automatically creates a <code class="literal">WebApplicationContext</code>
for Web Application Bundles and WAR files. When used in conjunction with an
an auto-configured Spring MVC <code class="literal">DispatcherServlet</code>,
there is generally no need to access the <code class="literal">WebApplicationContext</code>
programmatically, since all components of the web application are configured
within the scope of the <code class="literal">WebApplicationContext</code>
itself. However, if you wish to access the <code class="literal">WebApplicationContext</code>
you can do so via the web application&#8217;s <code class="literal">ServletContext</code>.
Virgo stores the bundle&#8217;s
<code class="literal">WebApplicationContext</code> in the ServletContext under
the attribute name "<code class="literal">BSN-ApplicationContext</code>", where
<code class="literal">BSN</code> is the <code class="literal">Bundle-SymbolicName</code>
of your WAR or Web Application Bundle.
</p><p>
Alternatively, since Virgo also stores the
<code class="literal">WebApplicationContext</code> under the attribute name
with the value of the <code class="literal">WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE</code>
constant, you may choose to use Spring MVC&#8217;s WebApplicationContextUtils&#8217;
<code class="literal">getWebApplicationContext(servletContext)</code>
or
<code class="literal">getRequiredWebApplicationContext(servletContext)</code>
methods to access the <code class="literal">WebApplicationContext</code> without providing
an explicit attribute name.
</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="developing-applications-programmatic-access-web-bundle-context"></a>Programmatic Access to the BundleContext</h4></div></div></div><p>
As required by the OSGi Web Applications specification, you can access the
<code class="literal">BundleContext</code> of your WAR or Web Application Bundle via the web application&#8217;s
<code class="literal">ServletContext</code>. The bundle context is stored in the
<code class="literal">ServletContext</code> under the attribute name <code class="literal">osgi-bundlecontext</code>.
</p></div></div></div><!--Begin LoopFuse code--><script src="http://loopfuse.net/webrecorder/js/listen.js" type="text/javascript"></script><script type="text/javascript">
_lf_cid = "LF_48be82fa";
_lf_remora();
</script><!--End LoopFuse code--><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch04s04.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="ch04.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="ch04s06.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4.4&nbsp;Creating and Using Configuration Artifacts&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;4.6&nbsp;Web Application Manifest Processing</td></tr></table></div></body></html>