The implicit object of application is javax servlet. An instance of ServletContext. It is mainly used to obtain initialization parameters and share attributes and their values throughout the JSP Application, which means that any attribute set by the implicit object of the application can be used for all JSP pages.
method:
- Object get property (string property name)
- void setAttribute (string attribute name, object)
- Invalid delete attribute (string object name)
- Enumerate getAttributeNames()
- String getInitParameter (string parameter name)
- Enumerate getInitParameterNames()
- String getrealpath (string value)
- Invalid log (string message)
- URL get resource (string value)
- InputStream getresourceasstream (string path)
- String getServerInfo()
- String getMajorVersion()
- String getMinorVersion()
- Object getAttribute(String attributeName): it returns the object stored in the given attribute name. For example, the following statement will return the object stored in the property "MyAttr".
String s = (String)application.getAttribute("MyAttr");
- Void setAttribute (string attributename, object): it sets the value of the attribute, or in other words, it stores the attribute and its value in the application context and can be used across JSP applications. Examples-
application.setAttribute("MyAttribute", "This is the value of Attribute");
The above statement will store the attribute and its value. If we use the following statement in any JSP page, what is the value of's'?
String s= (String) application.getAttribute("MyAttribute");
The value of String will be "This is the value of Attribute" because we have set it using the setAttribute method.
- void removeAttribute(String objectName): this method is used to delete the given attribute from the application. For example - it removes the attribute "MyAttr" from the application. If we try to get the value of the deleted attribute using the getAttribute method, it will return Null.
application.removeAttribute("MyAttr");
- Enumeration getAttributeNames(): this method returns an enumeration of all attribute names stored in the implicit object of the application.
Enumeration e= application.getAttributeNames();
- String getInitParameter(String paramname): it returns the initialization parameter value of the given parameter name. Example – web xml
<web-app> ... <context-param> <param-name>parameter1</param-name> <param-value>ValueOfParameter1</param-value> </context-param> </web-app>
Suppose the above is my web XML file
String s=application.getInitParameter("parameter1");
The value of s will be "ValueOfParameter1". Still confused about where it came from? See web Param value tag in XML file.
- Enumeration getInitParameterNames(): it returns an enumeration of all initialization parameters.
Enumeration e= application.getinitParameterNames();
- String getRealPath(String value): it converts the given path into an absolute path in the file system.
String abspath = application.getRealPath("/index.html");
The value of abspath will be the full http URL based on the existing file system.
- void log(String message): this method writes the given message to the default log file of the JSP engine (JSP container) associated with the application.
application.log("This is error 404 Page not found");
The above call will write the message "This is error 404 Page not found" to the default log file.
- String getServerInfo(): this method returns the name and version of the JSP container (JSP engine).
application.getServerInfo();
Application implicit object example
Use the application to capture the JSP page of the number of clicks. In this example, we use the application implicit object to calculate the number of clicks on the JSP page.
counter.jsp
<%@ page import="java.io.*,java.util.*" %> <html> <head> <title>Application Implicit Object Example</title> </head> <body> <% //Comment: This would return null for the first time Integer counter= (Integer)application.getAttribute("numberOfVisits"); if( counter ==null || counter == 0 ){ //Comment: For the very first Visitor counter = 1; }else{ //Comment: For Others counter = counter+ 1; } application.setAttribute("numberOfVisits", counter); %> <h3>Total number of hits to this Page is: <%= counter%></h3> </body> </html>
Output screenshot
The number of hits of the first visitor is 1.
When I refresh the page, the number of clicks increases.
If you like this tutorial, please share it with your friends.