zoukankan      html  css  js  c++  java
  • ServletConfig

    public interface ServletConfig {
        public String getServletName();//获取Servlet在web.xml文件中的配置名称,即<servlet-name>指定的名称
        public ServletContext getServletContext();//用来获取ServletContext对象
        public String getInitParameter(String name);//用来获取在web.xml中配置的初始化参数,通过参数名来获取参数值
        public Enumeration<String> getInitParameterNames();//用来获取在web.xml中配置的所有初始化参数名称
    
    }

    tomcat在创建Servlet对象时,会自动将初始化参数封装到ServletConfig对象中,传递给init()方法,就可以使用了

    如:

    <servlet>
            <servlet-name>sf</servlet-name>
            <servlet-class>com.sflik.servlet.HelloServlet</servlet-class>        
            <init-param>
            <param-name>pname</param-name>
            <param-value>pvalue</param-value>
            </init-param>        
        </servlet>
    @Override
        public void init(ServletConfig config) throws ServletException {
            System.out.println(config.getInitParameter("pname")); 
            System.out.println(config.getInitParameter("pvalue")); 
            Enumeration e = config.getInitParameterNames();
            while(e.hasMoreElements()){
                System.out.println(e.nextElement());
            }
        }

     GenericServlet实现了ServletConfig接口,拥有ServletConfig的方法

  • 相关阅读:
    11111 Generalized Matrioshkas
    Uva 442 Matrix Chain Multiplication
    Uva 10815 Andy's First Dictionary
    Uva 537 Artificial Intelligence?
    Uva 340 MasterMind Hints
    SCAU 9508 诸葛给我牌(水泥题)
    Uva 10420 List of Conquests(排序水题)
    Uva 409 Excuses, Excuses!
    10/26
    11/2
  • 原文地址:https://www.cnblogs.com/sflik/p/4564864.html
Copyright © 2011-2022 走看看