There are two types of init parameters.
1. Context init parameter which is accessible from all the servlets in a app. It can be accessed by following code.
getServletConfig().getServletContext().getInitParameter("companyName");
or
getServletContext().getInitParameter("companyName");
Deployment descriptor web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>TestInitParameters</display-name>
<context-param>
<param-name>companyName</param-name>
<param-value>Scotia bank Ltd</param-value>
</context-param>
</web-app>
2. Servlet init paramters which is accessible from a servlet for which it is declared. It can be accessed by following code.
getServletConfig().getInitParameter("supportEmailAddress");
annotation:
@WebServlet(
urlPatterns = { "/TestInitParameter" },
initParams = {
@WebInitParam(name = "supportEmailAddress", value = "support@gmail.com")
})
1. Context init parameter which is accessible from all the servlets in a app. It can be accessed by following code.
getServletConfig().getServletContext().getInitParameter("companyName");
or
getServletContext().getInitParameter("companyName");
Deployment descriptor web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>TestInitParameters</display-name>
<context-param>
<param-name>companyName</param-name>
<param-value>Scotia bank Ltd</param-value>
</context-param>
</web-app>
2. Servlet init paramters which is accessible from a servlet for which it is declared. It can be accessed by following code.
getServletConfig().getInitParameter("supportEmailAddress");
annotation:
@WebServlet(
urlPatterns = { "/TestInitParameter" },
initParams = {
@WebInitParam(name = "supportEmailAddress", value = "support@gmail.com")
})
No comments:
Post a Comment