日期:2014-05-18  浏览次数:20681 次

JSp页面中读取web.xml中的系统配置参数
用config.getInitParameter();读出的都是null

------------------------------------------
<%@page contentType="text/html; charset=GBK"%>
<%@page import="java.util.*"%>
<%
  String sqlConnectionStr = (String) config.getInitParameter("sqlConnectionStr");
  String uid = (String) config.getInitParameter("uid");
  String pwd = (String) config.getInitParameter("pwd");
%>
<html>
<head>
<title>index</title>
</head>
<body bgcolor="#ffffff">
<br>
  读取XML文件:
<br>
  连接字符串:
<%=sqlConnectionStr%><BR>
  用户名:
<%=uid %><BR>
  密码:
<%=pwd %><br>
<br>
<a href="XML.jsp">return</a>
</body>
</html>
-------------------------------------
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <display-name>TestReadXMLConfig</display-name>
  <servlet>
  <servlet-name>read</servlet-name>
  <servlet-class>test.read</servlet-class>
  <init-param>
  <param-name>sqlConnectionStr</param-name>
  <param-value>10.187.10.250</param-value>
  </init-param>
  <init-param>
  <param-name>uid</param-name>
  <param-value>sa</param-value>
  </init-param>
  <init-param>
  <param-name>pwd</param-name>
  <param-value>sa</param-value>
  </init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>readxml</servlet-name>
  <url-pattern>/readxml</url-pattern>
  </servlet-mapping>
</web-app>

------解决方案--------------------
config.getInitParameter 这个方法是不是能够得到你要的值了?
------解决方案--------------------
你用
<init-param> 
<param-name>sqlConnectionStr </param-name> 
<param-value>10.187.10.250 </param-value> 
</init-param>
试试,<init-param>标记与<servlet>标记同级