The Context Object

The context object is created when the servlet is initialised and is one of the properties of the servlet object. The context object holds various information and initialisation properties pertaining to the context in which the servlet is running. There may be more than one servlet running in a context, as such the contexts primary role is for data exchange between those servlets.

Shown below is the context object prototype chain.

The context object is of class Context.

Using the Context Object

The servlet properties and functions are accessed via the servlet by prefixing with ‘servlet.context’ as follows.

var si = servlet.context.info ;

var attrName = servlet.context.getAttribute("AttributeName");

Context Object Properties

All predefined properties are read-only and can not be changed by the script.

Property

Description

info

A string that details the contexts name and version number, for example Apache Tomcat 4.01

params

A set of context initialization parameters can be associated with the context to convey setup information such as a webmaster's email address. The params are held as an array of objects.

version

The version of the Servlet API that this servlet container supports. The version is culmination of two further properties of version ‘major’ and ‘minor’.

Context Object Functions

Function

Description

getAttribute(name)

Returns the context attribute identified by name or null if it does not exist.

getAttributeNames()

Returns an array containing the attribute names available within this servlet context.

setAttribute(name, value)

Binds an object ‘value’ to the  given attribute name in this servlet context.

getMimeType(fileName)

Returns the mime type for the filename specified.

getRealPath(pathname)

Returns the real path for the specified virtual pathname.

Comment on this topic

Topic ID: 150099