ServletConfig使用说明

在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。

Servlet:

public class ConfigServlet extends HttpServlet {

    ServletConfig config;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取指定的初始化参数
        String donwloadPath  = config.getInitParameter("donwloadPath");
        System.out.println("donwloadPath="+donwloadPath);
        response.getOutputStream().write(donwloadPath.getBytes());
        //循环获取所有初始化参数
        Enumeration<String> list = config.getInitParameterNames();
        while(list.hasMoreElements()){
            String name = list.nextElement();
            String value = config.getInitParameter(name);
            System.out.println(name+"="+value);
            String str = name+"="+value;
            response.getOutputStream().write(str.getBytes());
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doGet(request,response);
    }

    public void init(ServletConfig config) {
        this.config = config;
    }
}

web.xml:

    <!-- 测试servletConfig的使用 这里的init-param要放在servlet中-->
    <servlet>
        <servlet-name>ConfigServlet</servlet-name>
        <servlet-class>com.learn.servletconfig.ConfigServlet</servlet-class>
        <init-param>
            <param-name>donwloadPath</param-name>
            <param-value>/user/downloadPath</param-value>
        </init-param>
        <init-param>
            <param-name>name</param-name>
            <param-value>root</param-value>
        </init-param>
        <init-param>
            <param-name>password</param-name>
            <param-value>111111111</param-value>
        </init-param>       
    </servlet>
    <servlet-mapping>
        <servlet-name>ConfigServlet</servlet-name>
        <url-pattern>/config.action</url-pattern>
    </servlet-mapping>

运行结果:

donwloadPath=/user/downloadPath
password=111111111
donwloadPath=/user/downloadPath
name=root

ConfigServlet对象中有一个ServletConfig对象,其实这是不必要的。因为ConfigServlet继承了HttpServlet,HttpServlet又继承了GenericServlet 。GenericServlet 已经在内部维护了一个ServletConfig对象

GenericServlet源码如下:

public abstract class GenericServlet 
    implements Servlet, ServletConfig, java.io.Serializable
{
    private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
    private static ResourceBundle lStrings =
        ResourceBundle.getBundle(LSTRING_FILE);

    private transient ServletConfig config;

    public GenericServlet() { }
}

直接使用this.getServletConfig()方法:

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取指定的初始化参数        
        String donwloadPath  = this.getServletConfig().getInitParameter("donwloadPath");
        System.out.println("donwloadPath="+donwloadPath);
        response.getOutputStream().write(donwloadPath.getBytes());
        //循环获取所有初始化参数
        Enumeration<String> list = this.getServletConfig().getInitParameterNames();
        while(list.hasMoreElements()){
            String name = list.nextElement();
            String value = this.getServletConfig().getInitParameter(name);
            System.out.println(name+"="+value);
            String str = name+"="+value;
            response.getOutputStream().write(str.getBytes());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值