JSP - SPLessons

Chapter 20

JSP Application Object

JSP Application Object

The JSP Application Object is one of the implicit object type ServletContext and is created only once by the web application when the application is deployed on the web apps in Tomcat server. The JSP Application Object is used to get the information from the web.xml as context-param tag inside init-param. There are some methods to get the values from the web.xml. These methods are the same which are used in the servlet context chapter. Some of the important methods in JSP Application Object are:

Methods Return Type
getAttribute(string name) Object
getAttributeNames() java.util.Enumeration
getlnitparameter(string name) String
getlnitparameterNames() java.util.Enumeration
getRealPath(String path) string
getServerinfo() string
getMinorVersion() int
getMajorVersion() int
log(Message)() string

getAttribute(String name)

getAttribute(String name) method is used for returning attribute with specified name.

Syntax

application.getAttribute(String name);

getAttributeNames()(String name)

getAttributeNames() method is used for returning all attribute names within the application.

Syntax

application.getAttributeNames();

getlnitparameter(string name)

getlnitparameter(string name) method is used for returning intialiased parameter value. If parameter is not there then null value is returned.

Syntax

application.getInitParameter(String name);

getlnitparameterNames()(string name)

getlnitparameterNames() method is used for returning intialiased parameter name. 

Syntax

application.getInitParameterNames();

getRealPath(String path)

getRealPath(String path) method is used for translating the resource URL mentioned as parameter in the method into an input stream to read.

Syntax

application.getRealpath;

getServerinfo()

getServerinfo()method is used for returning name and version of the JRun servlet engine.

Syntax

application.getServerInfo();

getMinorVersion()

getMinorVersion()method is used for returning the minor version of the Servlet API for the JSP Container.

Syntax

application.getMinorVersion();

getMajorVersion()

getMajorVersion()method is used for returning the major version of the Servlet API for the JSP Container.

Syntax

application.getMajorVersion();

log(Message)()

log(Message)()method is used for writing text string to the JSP Container.

Syntax

application.log(Message);

The visibility of application is very high when compared to a page.

JSP Application Object- Following is an example

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>

This is the code to increase the count on the browser when refresh the page.

Output:

Output will be as follows.

While refreshing the page number hits will be increased as follows.

Summary

  • The application object is an occurrence of a class that executes the javax.servlet.ServletContext interface.
  • Application Object is uitilized to share the data with all application pages.