BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Application Implicit Object in JSP with examples

By Chaitanya Singh | Filed Under: JSP tutorial

In this tutorial, you will learn application implicit object in JSP. It is an instance of javax.servlet.ServletContext. It is used for getting initialization parameters and for sharing the attributes & their values across the entire JSP application, which means any attribute set by application implicit object is available to all the JSP pages.

Application implicit object methods

  • Object getAttribute(String attributeName)
  • void setAttribute(String attributeName, Object object)
  • void removeAttribute(String objectName)
  • Enumeration getAttributeNames()
  • String getInitParameter(String param_name)
  • Enumeration getInitParameterNames()
  • String getRealPath(String value)
  • void log(String message)
  • URL getResource(String value)
  • InputStream getResourceAsStream(String path)
  • String getServerInfo()
  • String getMajorVersion()
  • String getMinorVersion()
  1. Object getAttribute(String attributeName): It returns the object stored in a given attribute name. For example the following statement would return the object stored in attribute “MyAttr”.
    String s = (String)application.getAttribute("MyAttr");
  2. void setAttribute(String attributeName, Object object): It sets the value of an attribute or in other words it stores an attribute and its value in application context, which is available to use across JSP application. Example:
    application.setAttribute(“MyAttribute”, “This is the value of Attribute”);

    The above statement stores attribute and its value. What would be the value of ‘s’ if we use the following statement in any of the JSP page?

    String s= (String) application.getAttribute(“MyAttribute”);

    String s value would be “This is the value of Attribute” since we have set it using setAttribute method.

  3. void removeAttribute(String objectName): This method is used for removing the given attribute from the application. For example: It would remove the Attribute “MyAttr” from the application. If we try to get the value of a removed attribute using getAttribute method, it would return Null.
    application.removeAttribute(“MyAttr”);
  4. Enumeration getAttributeNames(): This method returns the enumeration of all the attribute names stored in the application implicit object.
    Enumeration e= application.getAttributeNames();
  5. String getInitParameter(String paramname): It returns the value of Initialization parameter for a given parameter name. Example:
    web.xml

    <web-app>
    …
    <context-param>
    <param-name>parameter1</param-name>
    <param-value>ValueOfParameter1</param-value>
    </context-param>
    </web-app>
    String s=application.getInitParameter(“parameter1”);

    The value of s will be “ValueOfParameter1”. Still confused where did it come from? See the param-value tag in above web.xml file.

  6. Enumeration getInitParameterNames(): It returns the enumeration of all the Initialization parameters.
    Enumeration e= application.getinitParameterNames();
  7. String getRealPath(String value): It converts a given path to an absolute path in the file system.
    String abspath = application.getRealPath(“/index.html”);

    The value of abspath would be a complete http URL based on the existing file system.

  8. void log(String message): This method writes the given message to the JSP Engine’s (JSP container’s) default log file associated to the application.
    application.log(“This is error 404 Page not found”);

    The above call would 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 JSP container (JSP Engine).
    application.getServerInfo();

JSP application implicit object Example

A JSP page to capture number of hits using application. In this example we are counting the number of hits to a JSP page using application implicit object.

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>

Screenshots of output
Number of hits is 1 for the first time visitor.
Application Implicit Object in JSP

Number of hits got increased when I refreshed the page.
Application Implicit Object in JSP

❮ JSP configJSP session ❯

Comments

  1. Rupa says

    April 9, 2014 at 2:30 AM

    Hi,

    Really i Love this Website,its very very useful,,,Thanks for the creator who posted such a good information,,

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE

Scripting Elements

  • Scriptlet Tag
  • Expression tag
  • Declaration tag

Implicit Objects

  • Implicit Objects
  • JSP Request
  • JSP Response
  • JSP Config
  • JSP Application
  • JSP Session
  • JSP Out
  • JSP pageContext
  • JSP Exception
  • Validate session

JSP directives

  • JSP Directives
  • Include Directive

JSP Exception

  • Exception handling

JSP Action

  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty

Expression language

  • Expression language

JSP Custom Tags

  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap