JSP inbuilt application object is of type ServletContext interface and it is implicitly available in the JSP code.
When using the JSP session object, you can track an individual session of each user accessing your application within a browser
using which the session was created but using JSP implicit application object,
you can track all the sessions of users with your application irrespective of the browser.
Note : JSP's application object has a global scope because its values persist even in between different browsers.
Some useful methods available to application object.
The methods in the table below are part of javax.servlet.ServletContext. interface. Hence, these methods are available to the JSP implicit
application object, which is a type of ServletContext.
This method gets an object stored in a session with a name, or null.
voidsetAttribute(String name, Object value)
This method sets an object with a name in a session.
This method removes an object with a name from the session.
This method returns a ServletContext object that corresponds to a specified URL.
This method returns a RequestDispatcher which acts as a wrapper for the resource at the path.
This method sets the time between client requests before the servlet container invalidates the session.
This method gets the creation time of the current session, since 1970 in milliseconds.
This method returns the ServletContext to the current session.
This method gives a true if the current session is new and client doesn't know about it.
Using JSP application object.
In the upcoming code, we will set the JSP's inbuilt application object with an attribute named Background_Color and its value Pink, by calling the ServletContext methods
using the implicit JSP application object.
In the first run of this page, its background color will be plain white i.e. the default background color of every web page. But on the second run of
this JSP page, we will set the html property bgcolor of the page using the JSP's application object attribute Background_Color,
which sets its to pink.
its background color will be changed to pink. Let's see the code.
<title>Using JSP application object</title>
<!-- html opening body tag starts here -->
Object ob = application.getAttribute("Background_Color");
out.println("bgcolor =" + application.getAttribute("Background_Color"));
> <!-- html opening body tag ends here -->
<h1>Setting and accessing application object</h1>
This page will set its background color using implicit application object of JSP.
executing this JSP page for the first time in any browser(Gooogle Chrome in our example), displays the web page with its plain white background color.
executing the same JSP page again in the same browser(Gooogle Chrome) displays the web page in a pink background color.
executing the same JSP page again in a different browser(Mozilla Firefox in the next picture) displays the web page in a pink background color because once
we set attribute of JSP's inbuilt application object, its value remains set and consistent in all the browsers.