Posted :
The ServletContext interface , defines methods , which can be used by a web application , in order to interact with the servlet container . A servlet container can pass initialization parameters , to a web application , by using what is called : context parameters.
Context
parameters , can be defined either inside the
web.xml
file , located in :
tomcat-version-number/conf/web.xml , in this case ,
it applies to every web application . Or it can be defined inside the
web.xml file , located in :
tomcat-version-number/webapps/web-app-name/WEB-INF/web.xml
, in this case , it will apply only , to the specified web application .
Context
parameters must be placed inside the web-app root tag , for example :
<!--
webapps/servletcontext-contex-param/WEB-INF/web.xml
The web application name is : servletcontext-contex-param
-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="false">
<context-param>
<param-name>web-application-initialization-param-1</param-name>
<param-value>web-application-initialization-param-1-value</param-value>
</context-param>
</web-app>
A
context parameter , must have a name , which is defined by
using the tag : param-name ,
and a value , which must be defined by using the tag : param-value .
To
get the names , of initialization parameters , defined for a web application ,
the getInitParameterNames method , of
ServletContext
can be used . The getInitParameterNames method of ServletContext , has
the following signature :
java.util.Enumeration<String> getInitParameterNames()
If
no context initialization parameters , have been defined ,
the
getInitParameterNames method , will return an empty
Enumeration . An empty Enumeration
does not contain any element .
To
get the value of a context initialization parameter ,
the getInitParameter method of
ServletContext , can be used . The
getInitParameter method of
ServletContext , has
the following signature :
java.lang.String getInitParameter(java.lang.String name)
If
the specified name does not exit , then
getInitParameter , will return
null .
For
example , the following jsp servlet , named :
index.jsp ,
retrieves the context initialization parameter ,
for a web application , named :
servletcontext-contex-param ,
which web.xml , was defined earlier .
No context initialization parameters ,
were defined inside the
tomcat-version-number/conf/web.xml
,
so the only context initialization parameters ,
are those defined , inside the
web.xml
of the
servletcontext-contex-param
web application .
<html>
<head>
<title>ServletContext Context Parameters</title>
</head>
<body>
<h1>getInitParameterNames</h1>
<!--Get all the context parameters names , passed to the web application ,
using 'application' implicit object .
The 'application' implicit object , is an instance of ServletContext .
The getInitParameterNames method , can be used to retrieve the names ,
of all the context initialization parameters , passed to a web application .
-->
<%
java.util.Enumeration<String> context_Init_Parameter_Names_En = application.getInitParameterNames();
if(context_Init_Parameter_Names_En.hasMoreElements()){
out.println("<ul>");
while(context_Init_Parameter_Names_En.hasMoreElements()){
out.println("<li>" + context_Init_Parameter_Names_En.nextElement() + "</li>");
}
out.println("</ul>");
}else{
out.println("<p>No initialization parameters , passed to the web application </p>");
}
%>
<h1>getInitParameter</h1>
<!--The getInitParameter method of ServletContext ,
can be used to retrieve the value ,
of a context initialization parameter ,
by providing the name of the
context initialization parameter .
If found , the value is returned
as a string , else the method will return null .
-->
<ul>
<%
String webMasterEmail = application.getInitParameter("web-master-email");
out.println("<li>Webmaster email is : "+ (webMasterEmail == null ? "undefined" : webMasterEmail) +"</li>");
String debugMode = application.getInitParameter("debug-mode");
out.println("<li>debugMode is : "+ (debugMode == null ? "undefined" : debugMode) +"</li>");
String WebApplicationInitializationParam1 = application.getInitParameter("web-application-initialization-param-1");
out.println("<li>WebApplicationInitializationParam1 is : "+ (WebApplicationInitializationParam1 == null ? "undefined" : WebApplicationInitializationParam1) +"</li>");
%>
</ul>
</body>
</html>
When
the web address
http://localhost:8080/servletcontext-contex-param/
is visited , the result is :
A context parameter , cannot be defined by using web annotation , inside a servlet , so it cannot be defined in this way :
@WebServlet(
urlPatterns = "/servlet-get-init-parameters",
initParams = {
@WebInitParam (name="an-init-param-name" , value="an-init-param-value")
}
)
public class ServletGetInitParameter extends HttpServlet {
In this code fragment , what was defined , is an initialization parameter , for a servlet . A servlet initialization parameter , applies only to a specific servlet , whereas a context initialization parameter , applies to the whole web application .
In
addition to defining context parameters
using : web.xml ,
context parameters can also be defined , by using the
setInitParameter method ,
of ServletContext .
The setInitParameter method , can be used inside a class , which implements the :
ServletContainerInitializer interface, or the
ServletContextListener
interface .
It can also be used inside the init method of
a servlet , which has
the load-on-startup tag inside web.xml ,
set to a value larger or equal , to 0 . When a servlet has the
load-on-startup tag , set to a value larger
or equal to 0 , the servlet will be loaded when the
web application is deployed .
For
example , a web application has the following
web.xml file :
<!--
webapps/servletcontext-contex-param/WEB-INF/web.xml
The web application name is : servletcontext-contex-param
-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="false">
<context-param>
<param-name>web-application-initialization-param-1</param-name>
<param-value>web-application-initialization-param-1-value</param-value>
</context-param>
<servlet>
<servlet-name>servlet-context-init-param-example</servlet-name>
<servlet-class>com.difyel.test.ServletContextInitParamExample</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servlet-context-init-param-example</servlet-name>
<url-pattern>/servlet-context-init-param-example</url-pattern>
</servlet-mapping>
</web-app>
The web.xml defines a servlet , named : servlet-context-init-param-example ,
the servlet class is defined to be com.difyel.test.ServletContextInitParamExample :
package com.difyel.test;
import java.util.Enumeration ;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletContext ;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletContextInitParamExample extends HttpServlet{
public void init(){
ServletContext servletContext = this.getServletContext();
servletContext.setInitParameter("context-init-param-name" , "context-init-param-value");
}
protected void doGet(HttpServletRequest req , HttpServletResponse res) throws ServletException , IOException{
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head><title>Servlet Context Init Param Example</title></head>");
out.println("<body>");
ServletContext servletContext = this.getServletContext();
out.println("<ul>");
Enumeration<String> contextInitParamNameEn = servletContext.getInitParameterNames();
if(contextInitParamNameEn.hasMoreElements()){
while(contextInitParamNameEn.hasMoreElements()){
String contextInitParamName = contextInitParamNameEn.nextElement();
String contextInitParamValue = servletContext.getInitParameter(contextInitParamName);
out.println("<li>"+ contextInitParamName + " : " + contextInitParamValue + "</li>");
}
}else{
out.println("<li>No context init parameters were defined for the web application </li>");
}
out.println("</ul>");
out.println("<body>");
out.println("<html>");
}
}
When
the web address
http://localhost:8080/servletcontext-contex-param/servlet-context-init-param-example/
is visited , the result is :
The example web application , can be download from here