Advertisement



< Prev
Next >



JSP application Object





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.

Methods Description
Object getAttribute(String name This method gets an object stored in a session with a name, or null.
void setAttribute(String name, Object value) This method sets an object with a name in a session.
void removeAttribute(String name) This method removes an object with a name from the session.
ServletContext getContext(String path) This method returns a ServletContext object that corresponds to a specified URL.
RequestDispatcher getRequestDispatcher(String filePath) This method returns a RequestDispatcher which acts as a wrapper for the resource at the path.
void setMaxInactiveInterval(int seconds This method sets the time between client requests before the servlet container invalidates the session.
long getCreationTime() This method gets the creation time of the current session, since 1970 in milliseconds.
ServletContext getServletContext() This method returns the ServletContext to the current session.
boolean isNew() This method gives a true if the current session is new and client doesn't know about it.



Advertisement




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.

Application.jsp
<html>

<head>
<title>Using JSP application object</title>
</head>


<!-- html opening body tag starts here -->
<body

<% 

Object ob = application.getAttribute("Background_Color");
boolean bool=false;

if(ob==null)
{
	bool=true;
}
else
{
	out.println("bgcolor =" + application.getAttribute("Background_Color"));
}

if(bool)
{
	application.setAttribute("Background_Color", "pink");
	
}

%>
> <!-- 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.
</body>

</html>



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.






Please share this article -




< Prev
Next >
< JSP session object
JSP config object >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement