JSP Application – Implicit Objects
In JSP, application is an implicit object of type ServletContext. This is an instance of javax.servlet.ServletContext. It is generated onetime by the web container when web application or web project is deployed on server.
This object is used to acquire the initialization parameter from the configuration file (web.xml). It is used for allotting the attributes and the values of the attributes over all JSP page. This indicates that any attribute which is set by application object will be accessible to all the JSP pages. In the application scope, it is also used to get, set, and remove attribute.
JSP application implicit object has scope in all jsp pages.
Methods of Application implicit object
void setAttribute(String attributeName, Object object) : It saves an attribute and its value in application context that is available to use over JSP application. For instance :
application.setAttribute(“Attribute”, “value of Attribute”);
The statement which is given above should have saved the attribute and its value.
Object getAttribute(String attributeName) : It gives back the object which is saved in a given attribute name. For instance, Lets have a look to the statement given in the above example. Now, what will be the value of ‘s’ if the below statement is used in any of the JSP page?
String s= (String) application.getAttribute(“Attribute”);
The value of ‘s’ will be “value of Attribute” after all we have put it with the help of setAttribute method and this value here is obtained using the getAttribute method.
void removeAttribute(String objectName) : For eliminating the given attribute from the application, This method is used. For instance : It will withdraw the Attribute “Attribute” from the application. If we attempt to retrieve the value of a withdrawn attribute with the help of getAttribute method then it will give back Null.
Enumeration getAttributeNames() : This method gives back the enumeration of all attribute names which are saved in the application object.
Enumeration e = application.getAttributeNames();
String getInitParameter(String paramname) : For a given parameter name, it gives back the value of Initialization parameter. Example : web.xml
Note: Here we are assuming that the above given file is web.xml file.
The value of ‘s’ will be “ValueOfParameter” which is given in the param-value tag in the configuration file (web.xml).
Enumeration getInitParameterNames() : The enumeration of all Initialization parameters is given by this method.
Enumeration e= application.getinitParameterNames();
String getRealPath(String value) : In the file system, it transforms a path given to a complete path.
String abspath = application.getRealPath(“/index.html”);
Based on the actual file system, the value of abspath will be an absolute http URL.
void log(String message) : This method sets down the given content to the JSP container’s default log file which is related to the application.
application.log(“Error 404, Page not found”);
To the default log file, the call above will writes the message “Error 404, Page not found”.
String getServerInfo() : This method gives back the name and version of JSP container.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.