ServletConfig对象

Posted tzzt01

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ServletConfig对象相关的知识,希望对你有一定的参考价值。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  
  <servlet>
    <servlet-name>ConfigDemo</servlet-name>
    <servlet-class>config.ConfigDemo</servlet-class>
    <!-- 初始参数: 这些参数会在加载web应用的时候,封装到ServletConfig对象中 -->
    <init-param>
        <param-name>path</param-name>
        <param-value>d:/contact.xml</param-value>
    </init-param>
    <init-param>
        <param-name>BBB</param-name>
        <param-value>BBB‘s value</param-value>
    </init-param>
     <init-param>
        <param-name>CCCC</param-name>
        <param-value>CCCC‘s value</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>ConfigDemo</servlet-name>
    <url-pattern>/ConfigDemo</url-pattern>
  </servlet-mapping>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

servlet:

package config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ConfigDemo extends HttpServlet {

    /*
     * 以下两段代码GenericServlet已经写了,我们无需编写(non-Javadoc)
     * 
     */
//    private ServletConfig config;
    
    /*
     *  1)tomcat服务器把这些参数会在加载web应用的时候,封装到ServletConfig对象中 
     *  2)tomcat服务器调用init方法传入ServletConfig对象
     */
//    public void init(ServletConfig config) throws ServletException {
//        this.config = config;
//    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 读取servlet的初始参数
        String path = this.getServletConfig().getInitParameter("path");
        
        File file = new File(path);
        // 读取内容
        BufferedReader br = new BufferedReader(new FileReader(file));
        String str = null;
        while( (str = br.readLine()) != null ) {
            System.out.println(str);
        }
        
        // 查询当前servlet的所有初始化参数
        Enumeration<String> enums = this.getServletConfig().getInitParameterNames();
        while(enums.hasMoreElements()) {
            String paramName = enums.nextElement();
            String paramValue = this.getServletConfig().getInitParameter(paramName);
            System.out.println(paramName + " = " + paramValue);
        }
        
        // 得到servlet的名称
        String servletName = this.getServletConfig().getServletName();
        System.out.println("servletName = " + servletName);
    }
}

 

以上是关于ServletConfig对象的主要内容,如果未能解决你的问题,请参考以下文章

ServletConfig类

ServletContext和ServletConfig

ServletContext和ServletConfig

ServletConfig接口

ServletConfig对象和ServletContext对象

ServletConfig