In this tutorial you will learn about the JSP application implicit Object and its application with practical example.
JSP application implicit Object
This is instance of ServletContext and it is created only once by the web container when our web application deploy on server. We need to initialize parameter in the web.xml file and also we can use application scope for this. This Parameter which is initialized in the web.xml can be used in all jsp pages.
Example:Let’s get understand it by an simple example.First we need to open our web.xml file so that we can put “context-params”. Please check below code.
#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 |
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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"> <servlet> <servlet-name>Mine</servlet-name> <jsp-file>/Welcome.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>Mine</servlet-name> <url-pattern>/Welcome</url-pattern> </servlet-mapping> <context-param> <param-name>SiteName</param-name> <param-value>w3adda.com</param-value> </context-param> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app> |
#Welcome.jsp: Here we will get our “context-param” data with the help of “application” implicit object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>W3Adda Application</title> </head> <body> <% String siteName = application.getInitParameter("SiteName"); out.print("Welcome " + siteName); %> </body> </html> |
#Output: So the output of this code will look like below image.