JSP Application Object
In this JSP tutorial, you will learn about application object, the methods available in application object, getAttribute(String name), getAttributeNames, setAttribute(String objName, Object object), removeAttribute(String objName), getMajorVersion(), getMinorVersion(), getServerInfo(), getInitParameter(String name), getInitParameterNames, getResourceAsStream(Path) and log(Message)
Application Object is used to share the data with all application pages. Thus, all users share information of a given application using the Application object. The Application object is accessed by any JSP present in the application. The class or the interface name of the object application is ServletContext.
The application object is written as:
Javax.servlet.http.ServletContext
Associated with the Application object are methods that provide details about the Container of JSP and utility methods.
Methods of Application Object:
There are numerous methods available for Application object. Some of the methods of Application object are:
- getAttribute(String name)
- getAttributeNames
- setAttribute(String objName, Object object)
- removeAttribute(String objName)
- getMajorVersion()
- getMinorVersion()
- getServerInfo()
- getInitParameter(String name)
- getInitParameterNames
- getResourceAsStream(Path)
- log(Message)
Detailed below is the usage of Application object along with syntax, example and explanation for some of the above methods.
getAttribute(String name):
The method getAttribute of Application object is used to return the attribute with the specified name. It returns the object given in parameter with name. If the object with name given in parameter of this getAttribute does not exist, then null value is returned.
General syntax of getAttribute method of Application object is as follows:
|
For example:
|
The above statement returns the object exforsys.
getAttributeNames:
The method getAttributeNames of Application object is used to return the attribute names available within the application. The names of attributes returned are an Enumeration.
General syntax of getAttributeNames method of Application object is as follows:
|
For example:
|
The above example returns the attribute names available within the current application as enumeration in exforsys.
setAttribute(String objName, Object object):
The method setAttribute of Application object is used to store the object with the given object name in the application.
General syntax of setAttribute method of Application object is as follows:
|
The above syntax stores the objname mentioned in String in the corresponding object mentioned as Object in the parameter of the setAttribute method.
For example:
|
In the above example, the object exvar is stored with the object name Exforsys in the application.
removeAttribute(String objName):
The method removeAttribute of Application object is used to remove the name of the object mentioned in parameter of this method from the object of the application.
General syntax of removeAttribute method of Application object is as follows:
|
For example:
|
The above statement removes the name from the object password of the application.
getMajorVersion():
The method getMajorVersion of Application object is used to return the major version of the Servlet API for the JSP Container.
General syntax of getMajorVersion method of Application object is as follows:
|
The returned value from the above method is an integer denoting the major version of the Servlet API.
For example:
|
|
The above statement gives 2 as the major version of the Servlet API in use for the Application object.
getMinorVersion():
The method getMinorVersion of Application object is used to return the minor version of the Servlet API for the JSP Container.
General syntax of getMinorVersion method of Application object is as follows:
|
The returned value from the above method is an integer denoting the minor version of the Servlet API.
For example:
|
|
The above gives 1 as the minor version of the Servlet API in use for the Application object.
getServerInfo():
The method getServerInfo of Application object is used to return the name and version number of the JRun servlet engine. Information about the JSP Container, such as, the name and product version, are returned by the method getServerInfo of Application object.
General syntax of getServerInfo method of Application object is as follows:
application.getServerInfo();
For example:
|
getInitParameter(String name):
The method getInitParameter of Application object is used to return the value of an initialization parameter. If the parameter does not exist, then null value is returned.
General syntax of getInitParameter method of Application object is as follows:
|
For example:
|
In the above, the value of initialization parameter eURL is retrieved and stored in string exforsys.
getInitParameterNames:
The method getInitParameterNames of Application object is used to return the name of each initialization parameter. The returned value is an enumeration. General syntax of getInitParameterNames method of Application object is as follows:
|
The returned value from the above method is an enumeration.
For example:
|
getResourceAsStream(Path):
The method getResourceAsStream of Application object is used to translate the resource URL mentioned as parameter in the method into an input stream to read. General syntax of getResourceAsStream method of Application object is as follows:
|
For example:
|
The above example translates the URL /exforsys.txt mentioned in the parameter of getResourceAsStream method into an input stream to read.
log(Message):
The method log of Application object is used to write a text string to the JSP Container’s default log file.
General syntax of log method of Application object is as follows:
|