Skip to content
Related Articles

Related Articles

Improve Article

JSP Application – Implicit Objects

  • Last Updated : 21 Aug, 2021
Geek Week

In JSP, application is an implicit object of type ServletContext. This is an instance of javax.servlet.ServletContext. It is generated onetime by the web container when web application or web project is deployed on server.

This object is used to acquire the initialization parameter from the configuration file (web.xml). It is used for allotting the attributes and the values of the attributes over all JSP page. This indicates that any attribute which is set by application object will be accessible to all the JSP pages. In the application scope, it is also used to get, set, and remove attribute.

JSP application implicit object has scope in all jsp pages.

Methods of Application implicit object

void setAttribute(String attributeName, Object object) :  It saves an attribute and its value in application context that is available to use over JSP application. For instance :

application.setAttribute(“Attribute”, “value of Attribute”);

The statement which is given above should have saved the attribute and its value. 



Object getAttribute(String attributeName) : It gives back the object which is saved in a given attribute name. For instance, Lets have a look to the statement given in the above example. Now, what will be the value of ‘s’ if the below statement is used in any of the JSP page?

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

The value of ‘s’ will be “value of Attribute” after all we have put it with the help of setAttribute method and this value here is obtained using the getAttribute method.

void removeAttribute(String objectName) : For eliminating the given attribute from the application, This method is used. For instance : It will withdraw the Attribute “Attribute” from the application. If we attempt to retrieve the value of a withdrawn attribute with the help of getAttribute method then it will give back Null.

 application.removeAttribute(“Attribute”);

Enumeration getAttributeNames() : This method gives back the enumeration of all attribute names which are saved in the application object.

 Enumeration e = application.getAttributeNames();

String getInitParameter(String paramname) : For a given parameter name, it gives back the value of Initialization parameter. Example : web.xml

XML




<web-app>
 
 
 
 <context-param>
 
 <param-name>parameter</param-name>
 
 <param-value>ValueOfParameter</param-value>
 
 </context-param>
 
 </web-app>

Note: Here we are assuming that the above given file is web.xml file.

 String s=application.getInitParameter("parameter");

The value of ‘s’ will be “ValueOfParameter” which is given in the param-value tag in the configuration file (web.xml).



Enumeration getInitParameterNames() : The enumeration of all Initialization parameters is given by this method. 

 Enumeration e= application.getinitParameterNames();

String getRealPath(String value) : In the file system, it transforms a path given to a complete path. 

 String abspath = application.getRealPath(“/index.html”);

Based on the actual file system, the value of abspath will be an absolute http URL.

void log(String message) : This method sets down the given content to the JSP container’s default log file which is related to the application. 

 application.log(“Error 404, Page not found”);

To the default log file, the call above will writes the message “Error 404, Page not found”.

String getServerInfo() : This method gives back the name and version of JSP container.  

 application.getServerInfo();

Example

index.html file 

HTML




<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
<form action="welcome">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
 
</body>
</html>

web.xml file

XML




<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <display-name>HelloWorld</display-name>
   
  <servlet>
  <servlet-name>welcome</servlet-name>
  <jsp-file>/welcome.jsp</jsp-file>
 
  </servlet>
   
  <servlet-mapping>
  <servlet-name>welcome</servlet-name>
  <url-pattern>/welcome</url-pattern>
  </servlet-mapping>
   
  <context-param>
  <param-name>dname</param-name>
  <param-value>com.mysql.jdbc.Driver</param-value>
  </context-param>
   
</web-app>

welcome.jsp 

HTML




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>GeeksforGeeks</title>
</head>
<body>
 
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=application.getInitParameter("dname");
out.print("<br/>driver name is="+driver); 
%>
 
</body>
</html>

Output

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :