• servlet中ServletConfig的使用


    转自:http://www.zzzj.com/html/20090117/69483.html

    前言

      相对于ServletContext,ServletConfig是针对特定的Servlet的参数或属性。ServletConfig是表示单独的Servlet的配置和参数,只是适用于特定的Servlet。从一个servlet被实例化后,对任何客户端在任何时候访问有效,但仅对本servlet有效,一个servlet的ServletConfig对象不能被另一个servlet访问

    1. 首先要设置初始化参数,如果只有一个特定的servlet要设定的参数(Servlet名字以及其它参数等),其它servlet不能共享,应该配置为ServletConfig参数,如一个读取附件的servlet要用到绝对目录,而别的servlet不会用到:

    <web-app>
        <servlet>
            <servlet-name>GetAtt</servlet-name>
            <servlet-class>mail.GetAttServlet</servlet-class>
            <init-param>
                <param-name>absPath</param-name>
                <param-value>/usr/mail/ax/axman/Maildir/</param-value>
            </init-param>
        </servlet>
    </web-app>

    2. 其次要取得ServletConfig对象:

    1). 从init()方法中得到:

    public class Test extends HttpServlet {
        ServletConfig config;
    
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
            this.config = config;
        }
    }

    2). 从getServletConfig()方法中得到:

    ServletConfig config=this.getServletConfig();
    if(config.getInitParameter("absPath").eaquals("adsd"){
       .......
    }

    3). 也可直接调用getInitParameter()方法获得参数值

  • 相关阅读:
    获取全部校园新闻
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    团队总结
    团队第二阶段冲刺绩效评估
    第二阶段冲刺第七天站立会议
    第二阶段冲刺第六天站立会议
    第二阶段冲刺第五天站立会议
    内测版本
    第二阶段冲刺第四天站立会议
  • 原文地址:https://www.cnblogs.com/tv151579/p/3597734.html
Copyright © 2020-2023  润新知