JSP application implicit Object

  • Application Context is an object of HttpServletContext and It is used pass information to the any Servlet which is present in Application.
  • Application Context object created by container when server started.
  • Application Context object is global to entire web application

Let’s understand it with Example

Project Structure

Application1.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
 
String f_name=application.getInitParameter("f_name");
String l_name=application.getInitParameter("l_name");
 
out.println("<h1>First Name: </h1>"+f_name);
out.println("<h1>Last Name: </h1>"+l_name);
 
%>
</body>
</html>

output:

Application2.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
<%@ page language="java" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
 
String f_name=application.getInitParameter("f_name");
String l_name=application.getInitParameter("l_name");
 
out.println("<h1>First Name: </h1>"+f_name);
out.println("<h1>Last Name: </h1>"+l_name);
 
%>
 
</body>
</html>

output:

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
	<display-name>JSPImplicitObject</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
 
 
	<servlet> 
	<servlet-name>JSP</servlet-name>
	<jsp-file>/ConfigObject.jsp</jsp-file>
	</servlet>
 
	<servlet-mapping>
	<servlet-name>JSP</servlet-name>
	<url-pattern>/JSP</url-pattern>
	</servlet-mapping>
 
 
	<context-param>
	<param-name>f_name</param-name>
	<param-value>Bhavesh</param-value>
	</context-param>
 
	<context-param>
	<param-name>l_name</param-name>
	<param-value>Lakhani</param-value>
	</context-param>
 
</web-app>

Leave a Reply

Your email address will not be published. Required fields are marked *