How can I implement global settings for a JSP application?

Luigi Viggiano

You can share information among all users of the active JSP application using the appliction object.
This object, is instantiated once per Java Virtual Machine, and can also be used to communicate with the servlet/jsp container.

There's a limitation: you can define a web site using more than one JVM running jsp/servlet servers, in this case you have more than one application, and then application object cannot be used as a centralized data storage for global settings.

In this case it's recommended to use a database, or jndi or a centralized xml configuration repository (this is the most recent fashion).

The application object exposes those methods:

  • Object getAttribute (String name)
  • Enumeration getAttributeNames()
  • int getMajorVersion()
  • int getMinorVersion()
  • String getMimeType(String file)
  • String getRealPath(String virtualpath)
  • URL getResource(String path) throws java.net.MalformedURLException
  • String getServerInfo()
  • log(String msg)
  • log(String message, Throwable throwable)
  • removeAttribute(String name)
  • setAttribute(String name,Object object)

See javadoc api for further informations.