• servlet的ServletConfig接口


    ServletConfig接口

          

    A servlet configuration object used by a servlet container to pass information to a servlet during initialization. 

    servlet 容器使用的 servlet 配置对象,该对象在初始化期间将信息传递给 servlet。

    web.xml配置

      <servlet>
      	<servlet-name>hello</servlet-name>
      	<servlet-class>com.qf.servlet.HelloServlet3</servlet-class>
      	<init-param>
      		<param-name>name</param-name>
      		<param-value>wxf</param-value>
      	</init-param>
      	<init-param>
      		<param-name>age</param-name>
      		<param-value>24</param-value>
      	</init-param>
      </servlet>
      <servlet-mapping>
      	<servlet-name>hello</servlet-name>
      	<url-pattern>/*</url-pattern>
      </servlet-mapping>
    

    servlet类编写

     1 public class HelloServlet extends HttpServlet {
     2 
     3     @Override
     4     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     5         System.out.println("doGet");
     6         testServletConfig();
     7     }
     8 
     9     public void testServletConfig() {
    10         
    11         //得到servlet配置对象 专门用于在配置servlet的信息
    12         ServletConfig config = getServletConfig();
    13         
    14         //获取到的是配置servlet里面servlet-name 的文本内容
    15         String servletName = config.getServletName();
    16         System.out.println("servletName==="+servletName);
    17         System.out.println("-----------");
    18         
    19         //获取ServletContext
    20         ServletContext context = config.getServletContext();
    21         String path = context.getContextPath();
    22         System.out.println("path==="+path);
    23         System.out.println("-----------");
    24         
    25         //获取某一具体的参数
    26         String name = config.getInitParameter("name");
    27         System.out.println("name==="+name);
    28         System.out.println("-----------");
    29         
    30         //获取所有的参数名称
    31         Enumeration<String> names = config.getInitParameterNames();
    32         //遍历取出所有的参数名称
    33         while (names.hasMoreElements()) {
    34             String key = (String) names.nextElement();
    35             String value = config.getInitParameter(key);
    36             System.out.println("key==="+key + "   value==="+value);
    37             
    38         }
    39     }
    40 }

    url(http://localhost:8080/HelloServlet/hello)访问后控制台输出

    doGet
    servletName===hello
    -----------
    path===/HelloServlet
    -----------
    name===wxf
    -----------
    key===name   value===wxf
    key===age   value===24
    

    ServletConfig的作用:可以在web.xml的<init-param>标签中配置一些变量值,并在servlet中根据param-name获取

  • 相关阅读:
    Java8基础学习之Object类
    Java8基础之equals方法和==的区别
    Spring集成ElasticSearch
    ElasticSearch常用的查询过滤语句
    数据库查看SQL执行计划
    数据库优化总结
    ElasticSearch之集群原理
    curl命令操作ElasticSearch总结
    ElasticSearch相关概念总结
    ElasticSearch基础入门
  • 原文地址:https://www.cnblogs.com/qf123/p/10042951.html
Copyright © 2020-2023  润新知