zoukankan      html  css  js  c++  java
  • Servlet 使用ServletConfig对象来配置Servlet

    ServletContext和ServletConfig的关联

      相同点:

        1、都可以用来配置Servlet

        2、都可以写在web.xml中。

      区别点:

        1、ServletContext对象,对于所有的Servlet来说,都是相同的,即,全局的。   而每一个Servlet单独拥有一个ServletConfig对象,不与其他Servlet共享。

        2、对于ServletConfig来说,配置项是写在web.xml中的servlet2中,而不是单独的提出来。

    ServletContext和ServletConfig的配置比较

      下面是在web.xml中配置的数据

    # 下面是ServletContext的全局配置项
    <context-param>
      <param-name>name</param-name>
      <param-value>张三</param-value>
    </context-param>
    <context-param>
      <param-name>age</param-name>
      <param-value>99</param-value>
    </context-param>
    
    # 下面是针对TestServletConfig这单独的servlet而设定的配置项
    <servlet>
      <servlet-name>TestServletConfig</servlet-name>
      <servlet-class>lixin.gan.TestServletConfig</servlet-class>
      <init-param>
      	<param-name>content</param-name>
      	<param-value>这是专门给TestServletConfig配置的数据</param-value>
      </init-param>
    </servlet>
    <servlet-mapping>
      <servlet-name>TestServletConfig</servlet-name>
      <url-pattern>/testServletConfig</url-pattern>
    </servlet-mapping>
    

      

    使用ServletConfig来获取配置项的值

    public class TestServletConfig extends HttpServlet {
    	@Override
    	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
    		ServletConfig config = this.getServletConfig();
    		
    		String content = config.getInitParameter("content");
    		System.out.println(content);
    		
    	}
    }
    

      

  • 相关阅读:
    C++ STL 一般总结(转载)
    Python高级语法总结
    Adaboost 算法的原理与推导——转载及修改完善
    简化版SMO算法标注
    【转载】机器学习算法基础概念学习总结
    C++中添加配置文件读写方法
    Python中scatter()函数--转载
    python 之 strip()--(转载)
    zabbix邮件报警脚本(Python)
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/-beyond/p/10082772.html
Copyright © 2011-2022 走看看