JSP implicit object "application"


 

JSP implicit object "application"

In this section, we will discuss about JSP implicit object 'application' and it's uses with an example.

In this section, we will discuss about JSP implicit object 'application' and it's uses with an example.

JSP IMPLICIT OBJECT application

In this section, we will discuss about JSP implicit object 'application' and it's uses with an example. Using "application"  object you can share data with all pages in an application. It means the "application" object is accessed by any JSP present in the application. The interface of "application" object is Javax.servlet.http.ServletContext.

Methods of implicit object application

  • getAttribute(String name)
  • getAttributeNames
  • setAttribute(String objName, Object object)
  • removeAttribute(String objName)
  • getMajorVersion()
  • getMinorVersion()
  • getServerInfo()
  • getInitParameter(String name)
  • getInitParameterNames
  • getResourceAsStream(Path)
  • log(Message)

(1) getAttribute(String name) : This method returns the 'object' stored in specified attribute name. If the specified parameter does not exist , it will return 'null' value.

Example :  application.getAttribute("roseindia");

(2) getAttributeNames() : This method returns all the attributes available within the application. The return  type is Enumeration.

Example : Enumeration roseindia;
                  roseindia = application.getAttributeNames();

(3) setAttribute(String objName, Object object) :  This method stores the 'object' in the object name provided(String objname) in the application.

Example : application.setAttribute("Variable", rosevar);

(4) removeAttribute(String objName) : This method is used to remove the specified attribute (String objName)  from the application.

Example :   application.setAttribute("password",password);
          application.removeAttribute("password");

(5) getMajorVersion() : This method is used to return the major version of the Servlet API for the JSP Container.

Example :     out.println("Major Version:"+application.getMajorVersion());

Output :        Major Version:2

The above statement gives 2 as the major version of the Servlet API in use for the Application object.

(6) getMinorVersion() : This method is used to return the minor version of the Servlet API for the JSP Container.

Example :     out.println("Minor Version:"+application.getMinorVersion());

Output :        Minor Version:1

The above statement gives 1 as the minor version of the Servlet API in use for the Application object.

(7) 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.

Example :   out.println("Server Information:"+application.getServerInfo());

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

Example : String Roseindia = application.getInitParameter("eURL");

In the above example, the value of initialization parameter "eURL" is retrieved and stored in string "Roseindia".

(9) getInitParameterNames() :  The method 'getInitParameterNames' of Application object is used to return the name of all initialization parameter of the application. The returned value is an enumeration.

Example :  Enumeration e;
           e=application.getInitParameterNames();

(10) 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.

Example :   InputStream stream = application.getResourceAsStream("/roseindia.txt")

(11)  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 :

          application.log(Message);

EXAMPLE :

0

applicationobject.jsp

<%@ page language="java" import="java.sql.*" %>

<%

String parameterValue="This is application object";

1

getServletContext().setAttribute("ApplicationValue","This is my Name");

String getValueApplcation=(String)getServletContext().getAttribute("ApplicationValue");

application.setAttribute("ApplcationVariable",parameterValue);

String getApplicationVariable=(String)application.getAttribute("ApplcationVariable");

%>

<html>

<head>

<title>Application object in JSP</title>

</head>

<body>

<font color="red">The value of getServletContext application object :

</font><font color="blue"><%=getValueApplcation%></font><br>

2

<font color="red">

This is application object :</font><font color="blue"><%=getApplicationVariable%>

</font></body>

3

</html>

OUTPUT :

Download Source Code

4

Ads