jsp如何获得web.xml中初始化配置信息,获取得web.xml中初始化配置信息的实例源码介绍,获取web.xml文件中相应的参数名以及参数值的方法实例:
<%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>My JSP ‘JSP_applecation_init.jsp’ starting page</title>
</head>
<body>
<table border=”0″ width=”60%”>
<tr>
<th>参数名name</th>
<th>参数值value </th>
</tr>
<%
//获得web.xml中初始化配置信息的枚举
Enumeration enumeration = application.getInitParameterNames();
//判断获得的枚举不为空
if(enumeration != null){
//循环遍历枚举
while(enumeration.hasMoreElements()){
//获取参数名
String name = (String)enumeration.nextElement();
//根据参数名,获取参数值
String value = (String)application.getInitParameter(name);
%>
<tr>
<td align=”center”><%=name%></td>
<td align=”center”><%=value%></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
web.xml文件如下:
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.5″
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_2_5.xsd”>
<!– 设置初始化参数 –>
<context-param>
<param-name>datase_host</param-name>
<param-value>localhost</param-value>
</context-param>
<context-param>
<param-name>username</param-name>
<param-value>XXX</param-value>
</context-param>
<context-param>
<param-name>email</param-name>
<param-value>XXX@123.com</param-value>
</context-param>
<context-param>
<param-name>address</param-name>
<param-value>The global</param-value>
</context-param>
</web-app>