5

I am used to configuring web application in context.xml, including jdbc resorces, and application configuration parameters.

Under glassfish, what is considered the standard place to store application configuration information?

We used to store it in the context.xml file as follows:

<Resource
url="jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=utf8"
username="username"
name="jdbc/db"
password="secret"
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
maxActive="5" maxIdle="2" maxWait="10000"
removeAbandoned="true"
type="javax.sql.DataSource"
/>

<Parameter name="application.url" value="http://localhost:8080/News/" override="false"/>
<Parameter name="smtp.server" value="smtp.example.com" override="false"/>
<Parameter name="smtp.port" value="25" override="false"/>
<Parameter name="smtp.from.address" value="admin@example.com" override="false"/>
<Parameter name="smtp.from.name" value="Site administrator" override="false"/>
<Parameter name="list.name" value="DEV" override="false"/>
<Parameter name="temporary.folder" value="/tmp" override="false"/>
<Parameter name="authentication.type" value="LDAP" override="false"/>
corydoras
  • 7,130
  • 12
  • 55
  • 58

5 Answers5

2

It sounds like you want "glassfish-resources.xml". You can create JDBC and database pool resources, JavaMail resources, etc there.

http://javahowto.blogspot.com/2011/02/sample-glassfish-resourcesxml.html

ringerc
  • 21
  • 2
2

For all who are looking for a way to set context parameters in Glassfish - it is possible.

While Glassfish does not support a context.xml file, you can still set/override web app context parameters. There is the asadmin sub command set-web-context-param to set these. The values will be stored in your domain.xml - the will not be lost on redeploy of the application.

Synopsis (from documentation link above):

set-web-context-param [--help] --name=context-param-name 
    {--value=value|--ignoredescriptoritem={false|true}} 
    [--description=description] application-name[/module]

Resources such as JDBC datasources can be configured with asadmin, too, or you can use the web gui of glassfish. All that information will be stored in the domain.xml file.

Michael Paesold
  • 557
  • 4
  • 12
  • I think they will be lost if you undeploy and then deploy an application, though... – vkraemer Jun 20 '13 at 04:43
  • Thank you MichaelPaesold , I was trying to add the [context-param to the domain.xml](https://docs.oracle.com/cd/E19226-01/820-7694/giuiv/index.html) manually but I wasn't understanding where I should add it. This command does this automatically. It would be nicer if it could be configured on the web gui. @vkraemer There's no real need to undeploy an application to deploy again, we can redeploy directly or just disable it for some time. The configurations are only lost if we undeploy, it's kept if we redeploy using the "redeploy" functionality – Polyana Fontes Apr 27 '16 at 17:15
1

In your app's web.xml, per Java EE specs.

Here is tomcat's blurb about context.xml.

You can configure named values that will be made visible to the web application as servlet context initialization parameters by nesting elements inside this element. For example, you can create an initialization parameter like this:

<Context ...>   ...   
<Parameter name="companyName" value="My Company, 
    Incorporated" override="false"/>
</Context>

This is equivalent to the inclusion of the following element in the web application deployment descriptor (/WEB-INF/web.xml):

<context-param>   
    <param-name>companyName</param-name>  
    <param-value>My Company, Incorporated</param-value>
</context-param>
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
alphazero
  • 27,094
  • 3
  • 30
  • 26
  • This works for parameters, but how about for setting up database connection pools? – corydoras Nov 06 '09 at 04:45
  • Connection pools are defined using the admin gui. But if you need your xml fix, its all in sun-web.xml. – alphazero Nov 06 '09 at 14:45
  • 1
    Setting aside the fact I am quite offended you think I like XML. The point is that database connections should not have to be manually configured every time you deploy an application. With tomcat you just setup the config details and you can deploy to the server without having to screw around with GUI interfaces. Add to that the possibility that users playing with the admin interface may forget to do things like turn on UTF-8 support on the JDBC connection pool and its a recipe for disaster. – corydoras Nov 08 '09 at 21:36
0

You could use JNDI properties instead. I believe that Glassfish uses a jndi.properties file.

http://docs.sun.com/app/docs/doc/820-4336/gcpge?a=view

Kaleb Brasee
  • 51,193
  • 8
  • 108
  • 113
0

Turns out the answer is there is no alternative to the section in tomcats context.xml.

It turns out that the element is a tomcat specific thing generally not supported by other app servers such as glassfish.

As far as I can tell there seems to be no simple/sensible alternative apart from manually configuring database resources through either GUI or command line interfaces.

corydoras
  • 7,130
  • 12
  • 55
  • 58
  • 1
    As unfortunate as it is, that's just how it works. Kinda like how at work we have an orion-application.xml file to configure things on Oracle Application Server. – Powerlord Nov 16 '09 at 22:08