5.在JSP中取得在WEB.XML中定义的参数:在WEB.XML文件中设置参数:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name> myjsp </servlet-name>
<jsp-file> /MyJsp.jsp </jsp-file>
<init-param>
<param-name>firstparam</param-name>
<param-value>firstparamvalue</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name> myjsp </servlet-name>
<url-pattern> /myurl/* </url-pattern>
</servlet-mapping>
</web-app>
下面的程序无法取出在WEB.XML中定义的参数firstparam的值firstparamvalue
<html>
<HEAD><TITLE>my jsp page .jsp</TITLE></HEAD>
My init parameter is <%= application.getInitParameter("firstparam") %>
</html>
下面的程序就可以取出在WEB.XML中定义的参数firstparam的值firstparamvalue
<%
String Str1;
Str1=config.getInitParameter("firstparam");
Out.println(Str1);
%>
因为config是类javax.servlet.ServletConfig的对象,而application 是类javax.servlet.ServletContext的对象.