Servlet That Displays It's Initial Parms From web.xml
There are two types of initialization parms that can be passed to a Servlet
from the web.xml file.
The first is from what is called the ServletContext. These are available to
all Servlets.
The second is from what is called the ServletConfig. It is tailored for each
Servlet separately.
Here is a part of a web.xml file that is used to pass parameters via the ServletContext
to
all Servlets:
<context-param>
<param-name>db.pooltries</param-name>
<param-value>200</param-value>
</context-param>
<context-param>
<param-name>db.timeout</param-name>
<param-value>90</param-value>
</context-param>
On the other hand, here is a part of a web.xml file that is used to pass parameters
via the ServletConfig
to a specific Servlet:
<servlet>
<servlet-name>Demo2Servlet</servlet-name>
<servlet-class>Demo2Servlet</servlet-class>
<init-param>
<param-name>db.maxconn</param-name>
<param-value>20</param-value>
</init-param>
<init-param>
<param-name>db.maxuserconn</param-name>
<param-value>5</param-value>
</init-param>
</servlet>
Here's the whole servlet called Demo2Servlet reads from both sources
of paraemters,
and writes their values to the screen:
//set classpath for compile with . loc_setClasspat in $ROOT/etc dir
//do compile with javac Demo2Servlet.java
//in browser, http://server:port/app/servlet/DemoServlet
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;public class Demo2Servlet extends HttpServlet{ public void service (ServletRequest req , ServletResponse res) throws IOException{ res.setContentType("text/html"); PrintWriter out = res.getWriter();ServletContext context = getServletContext(); out.println("Displaying context parameters <BR>");Enumeration p1 = context.getInitParameterNames(); while (p1.hasMoreElements()) { String parm = (String)p1.nextElement(); out.println(parm + " = " + context.getInitParameter(parm) + "<BR>"); }ServletConfig config = getServletConfig(); out.println("<BR>Init params for " + config.getServletName() +"<BR>");Enumeration p2 = config.getInitParameterNames(); while (p2.hasMoreElements()) { String parm = (String)p2.nextElement(); out.println(parm + " = " + config.getInitParameter(parm) + "<BR>"); } } }
Displaying context parameters
db.pooltries = 200
db.timeout = 90Init params for Demo2Servlet
db.maxconn = 20
db.maxuserconn = 5
And here is the whole web.xml file:
<web-app>
<display-name>Demo2 Servlet</display-name>
<description>Shows Reading Parms From web.xml </description>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
<welcome-file>
index.html
</welcome-file>
</welcome-file-list><context-param> <param-name>db.pooltries</param-name> <param-value>200</param-value> </context-param> <context-param> <param-name>db.timeout</param-name> <param-value>90</param-value> </context-param><servlet> <servlet-name>Demo2Servlet</servlet-name> <servlet-class>Demo2Servlet</servlet-class> <init-param> <param-name>db.maxconn</param-name> <param-value>20</param-value> </init-param> <init-param> <param-name>db.maxuserconn</param-name> <param-value>5</param-value> </init-param> </servlet></web-app>