Skip to main content

Context (webapp) configuration in GlassFish

Posted by amyroh on May 23, 2007 at 4:33 PM EDT
Deployment specific web application configuration using context.xml in GlassFish was added a couple of months ago but related questions keep coming up on mailing lists and forum so I've decided to blog about it so it's easy to point out. ;-)

Similar to Tomcat you can define your context.xml as follows.

1. Global context.xml glassfish/domains/domain1/config/context.xml which will be loaded by *all* webapps.

For example, global context.xml can define global environment entry to be shared across all webapps as follows.

<Context>
<Environment name="global_environment" type="java.lang.String" value="global_environment_value"/>
</Context>


2. Virtual server context.xml specified by virtual-server property "contextXmlDefault" in domain.xml will be loaded by all webapps of this virtual server.

For examples, you can define contextXmlDefault for a virtual server in your domain.xml as follows.

<virtual-server id="server" ... >
<property name="contextXmlDefault" value="config/context.xml.default"/>
</virtual-server>


3. Webapp specific context.xml will be loaded by this webapp only.

You can define /META-INF/context.xml in a WAR file for this webapp specific environment and resource definitions.

<Context>
<Environment name="webapp-env" type="java.lang.String" value="webapp-env-value"/>
<Resource name="jdbc/__default" auth="Container" type="javax.sql.DataSource" description="Default Database"/>
</Context>


GlassFish V2 Beta 2 or later should have this feature.

Cheers,
Amy
Related Topics >>

Comments

<p>This is a great post; it was very informative. I look ...

This is a great post; it was very informative. I look forward in reading more of your work. Also, I made sure to bookmark your website so I can come back later. I enjoyed every moment of reading it. internet marketing
testking

Why using a context.xml resource like in Tomcat ?

This is actually an issue as the context.xml is not a standard J2EE resource but proprietary... so now it is overlapping the context of tomcat, which means I cannot have the same web application deployed on glassfish and tomcat using Netbeans. I am constrained to duplicate the project just to have two different context.xml as they are not compliant at all (see realm settings) Why not sun-context.xml ? All sun specific server resources have been always starting with sun-... Overlapping is not a good thing. Roberto