[untitled] Application implicit object and example in JSP

Posted by sandrol76 on Sun, 30 Jan 2022 03:21:29 +0100

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.


  • 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()
  1. 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");
  2. 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.

  3. 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.
  4. Enumeration getAttributeNames(): this method returns an enumeration of all attribute names stored in the implicit object of the application.
        Enumeration e= application.getAttributeNames();
  5. String getInitParameter(String paramname): it returns the initialization parameter value of the given parameter name. Example – web xml

    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.

  6. Enumeration getInitParameterNames(): it returns an enumeration of all initialization parameters.
        Enumeration e= application.getinitParameterNames();
  7. 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.

  8. 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.

  9. String getServerInfo(): this method returns the name and version of the JSP container (JSP engine).

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.


<%@ page import="java.io.*,java.util.*" %>
<title>Application Implicit Object Example</title>
 //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;
 //Comment: For Others 
 counter = counter+ 1;
 application.setAttribute("numberOfVisits", counter);
<h3>Total number of hits to this Page is: <%= counter%></h3>

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.

Topics: Java Javascript elementUI