zoukankan      html  css  js  c++  java
  • ServletConfig对象

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

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

    ServletConfig接口提供的方法:

    Method Summary
     String getInitParameter(String name)
              Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
     Enumeration getInitParameterNames()
              Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
     ServletContext getServletContext()
              Returns a reference to the ServletContext in which the caller is executing.
     String getServletName()
              Returns the name of this servlet instance.

    代码演示:

     1 package com.itheima.action;
     2 
     3 import java.io.IOException;
     4 import java.util.Enumeration;
     5 
     6 import javax.servlet.ServletConfig;
     7 import javax.servlet.ServletException;
     8 import javax.servlet.http.HttpServlet;
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 
    12 /**
    13  * Servlet implementation class ServeletDemo13
    14  */
    15 public class ServletDemo13 extends HttpServlet {
    16     private static final long serialVersionUID = 1L;
    17 
    18     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    19         // 0.获取ServletConfig对象
    20         ServletConfig config = getServletConfig();
    21         // 1.根据指定参数名获取参数值
    22         String username = config.getInitParameter("username");
    23         String password = config.getInitParameter("password");
    24         System.out.println(username + " " + password);
    25         // 2.获取所有初始化参数名
    26         Enumeration<String> names = config.getInitParameterNames();
    27         while (names.hasMoreElements()) {
    28             String name = names.nextElement();
    29             // 根据已获得的参数名获取参数值
    30             String value = config.getInitParameter(name);
    31             System.out.println(name + "---" + value);
    32         }
    33         //    3.获取Servlet名称
    34         String servletName = config.getServletName();
    35         System.out.println(servletName);
    36         
    37         // 4.获取contextpath
    38         String contextPath = config.getServletContext().getContextPath();
    39         System.out.println(contextPath);
    40     }
    41 
    42     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    43         doGet(request, response);
    44     }
    45 
    46 }

    web.xml相关配置如下:
      

     1 <servlet>
     2     <description></description>
     3     <display-name>ServletDemo13</display-name>
     4     <servlet-name>ServletDemo13</servlet-name>
     5     <servlet-class>com.itheima.action.ServletDemo13</servlet-class>
     6     <init-param>
     7         <param-name>username</param-name>
     8         <param-value>root</param-value>
     9     </init-param>
    10     <init-param>
    11         <param-name>password</param-name>
    12         <param-value>abc</param-value>
    13     </init-param>
    14   </servlet>
    15   <servlet-mapping>
    16     <servlet-name>ServletDemo13</servlet-name>
    17     <url-pattern>/ServletDemo13</url-pattern>
    18   </servlet-mapping>
  • 相关阅读:
    lsblk---列出所有可用块设备的信息,
    blkid---对系统块设备信息查询
    du---是对文件和目录磁盘使用的空间查看
    strings---对象文件或二进制文件中查找可打印的字符串
    which---查找并显示给定命令的绝对路径
    whereis---定位指令的二进制程序、源代码文件和man手册页等相关文件的路径。
    cd---切换工作目录
    cp---复制文件
    ls---显示文件目录各项信息
    pwd---以绝对路径的方式显示用户当前工作目录
  • 原文地址:https://www.cnblogs.com/datapool/p/6829773.html
Copyright © 2011-2022 走看看