JavaEE中关于ServletConfig的小结
在servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到servletconfig对象中,并在调用servlet的init方法时,servletconfig对象传递给servlet。进而,程序员通过servletconfig对象就可以得到当前servlet的初始化参数信息。
示例代码如下:
package com.yyz.servletconfig;
import java.io.ioexception;
import java.util.enumeration;
import javax.servlet.servletconfig;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
public class servletconfigdemo1 extends httpservlet {
servletconfig config;
public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
//获取指定的初始化参数
string value = config.getinitparameter("xxx");
response.getoutputstream().write(value.getbytes());
//获取所有的初始化参数
enumeration e = cofig.getinitparameternames();
while(e.hasmoreelements()){
string name = (string) e.nextelement();
value = config.getinitparameter(name);
response.getoutputstream().write((name+"="+value+"<br/>").getbytes());
}
}
public void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
doget(request,response);
}
@override
public void init(servletconfig config) throws servletexception {
this.config = config;
}
}
相应的web.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns=""
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation=" "
version="2.5">
<servlet>
<servlet-name>servletconfigdemo1</servlet-name>
<servlet-class>com.yyz.servletconfig.servletconfigdemo1</servlet-class>
<init-param>
<param-name>xxx</param-name>
<param-value>yyy</param-value>
</init-param>
<init-param>
<param-name>name</param-name>
<param-value>yyz</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>yyy</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletconfigdemo1</servlet-name>
<url-pattern>/servlet/servletconfigdemo1</url-pattern>
</servlet-mapping>
</web-app>
测试结果如下:
在上面的代码中,servletconfigdemo1对象中有一个servletconfig对象,其实这是不必要的。因为servletconfigdemo1继承了httpservlet,httpservlet又继承了genericservlet 。genericservlet 已经在内部维护了一个servletconfig对象。相关实现如下:
public abstract class genericservlet
implements servlet, servletconfig, java.io.serializable
{
… …
private transient servletconfig config;
public servletconfig getservletconfig() {
return config;
}
}
因而我们可以通过我们写的servlet对象的getservletconfig()方法直接拿到servletconfig对象,示例代码如下:
package com.yyz.servletconfig;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
public class servletconfigdemo2 extends httpservlet {
public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
string value = this.getservletconfig().getinitparameter("name");
system.out.println(value);
}
public void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
doget(request, response);
}
}
web.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns=""
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation=" "
version="2.5">
<servlet>
<servlet-name>servletconfigdemo2</servlet-name>
<servlet-class>com.yyz.servletconfig.servletconfigdemo2</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>yyz</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletconfigdemo2</servlet-name>
<url-pattern>/servlet/servletconfigdemo2</url-pattern>
</servlet-mapping>
</web-app>