Home  >  Article  >  Java  >  What is ServletConfig

What is ServletConfig

(*-*)浩
(*-*)浩Original
2019-05-05 18:17:417262browse

ServletConfig refers to the configuration information of the current servlet in the web.xml file. Developers can obtain the initialization parameter information of the current servlet through the ServletConfig object.

What is ServletConfig




What is ServletConfig

############################################################################################################################################# I just started to contact the Spring MVC framework. After understanding the framework, I really wanted to know how the framework runs, so I slowly fumbled around and looked at the source code. The first stumbling block is Servletconfig. Found often in servlet implementation classes. Not only do you have to ask what is this for? ######Recommended course: ###Java tutorial############ServletConfig#########ServletConfig represents the configuration information of the current servlet in web.xml ######
String getServletName()  -- 获取当前Servlet在web.xml中配置的名字
String getInitParameter(String name) -- 获取当前Servlet指定名称的初始化参数的值
Enumeration getInitParameterNames()  -- 获取当前Servlet所有初始化参数的名字组成的枚举
ServletContext getServletContext()  -- 获取代表当前web应用的ServletContext对象
###In the Servlet configuration file, you can use one or more tags to configure some initialization parameters for the servlet. #########When the servlet is configured with initialization parameters, the web container will automatically encapsulate these initialization parameters into the ServletConfig object when creating the servlet instance object, and call When calling the servlet's init method, pass the ServletConfig object to the servlet. Furthermore, programmers can obtain the initialization parameter information of the current servlet through the ServletConfig object. #########The advantage of this is: if you put the database information, encoding method and other configuration information in web.xml, if the user name and password of the database change in the future, you can directly and easily modify the web .xml is enough, avoiding the trouble of directly modifying the source code. ######Code example: ###
<servlet>
        <servlet-name>ServletConfigTest</servlet-name>
        <servlet-class>com.vae.servlet.ServletConfigTest</servlet-class>
        <init-param>
            <param-name>name1</param-name>
            <param-value>value1</param-value>
        </init-param>
        <init-param>
            <param-name>encode</param-name>
            <param-value>utf-8</param-value>
        </init-param>
</servlet>
###Then get the above two parameters in the code. The code implementation is as follows: ###
package com.vae.servlet;
 
import java.io.IOException;
import java.util.Enumeration;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class ServletConfigTest extends HttpServlet {
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        ServletConfig config = this.getServletConfig();  //拿到init方法中的ServletConfig对象
 
        // --获取当前Servlet 在web.xml中配置的名称(用的不多)
         String sName = config.getServletName();
         System.out.println("当前Servlet 在web.xml中配置的名称:"+sName);
         
        // --获取当前Servlet中配置的初始化参数(只能获取一个)经常用到
        // String value = config.getInitParameter("name2");
        // System.out.println(value);
 
        // --获取当前Servlet中配置的初始化参数(全部获取)经常用到
         Enumeration enumration = config.getInitParameterNames();
         while(enumration.hasMoreElements()){
         String name = (String) enumration.nextElement();
         String value = config.getInitParameter(name);
         System.out.println(name+":"+value);
         }
    }
 
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
###The core code is line 17. Get the ServletConfig object in the init method through this.getServletConfig() method, and then obtain the configuration information. ######Run the program and the background print log is as follows: ############

The above is the detailed content of What is ServletConfig. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn