Servlet technology 9_ServletConfig class

SerlvetConfig: Servlet program configuration information class

effect:

  1. Get the value of the alias servlet-name of the servlet program (servletConfig.getServletName())

  2. Get the initialization parameter init-param (servletConfig.getInitParameter("parameter name"))

  3. Get the ServletContext object (servletConfig.getServletContext())

In the init initialization method:

@Override
    public void init(ServletConfig servletConfig) throws ServletException {
    
    
        System.out.println("2.初始化方法");

//        1. 获取Servlet程序的别名 servlet-name 的值
        System.out.println("HelloServlet程序的别名是:"+servletConfig.getServletName());
//        2. 获取初始化参数 init-param
        System.out.println("HelloServlet程序的初始化参数username的值是:"+servletConfig.getInitParameter("username"));
        System.out.println("HelloServlet程序的初始化参数url的值是:"+servletConfig.getInitParameter("url"));
//        3. 获取 ServletContext 对象
        System.out.println("servletConfig.getServletContext: "+servletConfig.getServletContext());
    }

Among them, the initialization parameter init-param needs to configure the web.xml configuration file:

Modify the corresponding servlet tag (multiple init-param can be set):

<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.servlet1.HelloServlet</servlet-class>

    <!-- init-param是初始化参数 -->
    <init-param>
        <!-- param-name是参数名 -->
        <param-name>username</param-name>
        <!-- param-value是参数值 -->
        <param-value>root</param-value>
    </init-param>

    <init-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/test</param-value>
    </init-param>

</servlet>

supplement:

Both the Servlet program and the ServletConfig object are created by Tomcat, and we are responsible for using them.

Servlet programs are created by default when they are accessed for the first time, and ServletConfig is a corresponding ServletConfig object created when each Servlet program is created.

Initial configuration information is encapsulated in ServletConfig

In addition to using ServletConfig in init initialization, we can also use it in other places

For example, in the doGet or getPost method of HelloServlet2

    /**
     * doGet()在get请求的时候调用
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("HelloServlet2的doGet方法");
        // 也可以使用ServletConfig对象
        ServletConfig servletConfig = getServletConfig();
        System.out.println(servletConfig);
//        1. 获取Servlet程序的别名 servlet-name 的值
        System.out.println("HelloServlet程序的别名是:"+servletConfig.getServletName());
//        2. 获取初始化参数 init-param
        System.out.println("HelloServlet程序的初始化参数username的值是:"+servletConfig.getInitParameter("username"));
        System.out.println("HelloServlet程序的初始化参数url的值是:"+servletConfig.getInitParameter("url"));
//        3. 获取 ServletContext 对象
        System.out.println("servletConfig.getServletContext: "+servletConfig.getServletContext());

    }

But note: each ServletConfig corresponds to its own servlet program, and you cannot get other people's servlet information in other people's servlet programs (HelloServlet2 cannot get the information in HelloServlet1)

Insert picture description here

If you override the init method in HelloServlet2:

@Override
public void init(ServletConfig config) throws ServletException {
    System.out.println("从写了init初始化方法,做了一些初始化工作");
}

Run result: found a null pointer exception

Insert picture description here

The reason for the error: be sure to call super.init(config) in the rewritten init method

@Override
public void init(ServletConfig config) throws ServletException {
    
    
    super.init(config);
    System.out.println("从写了init初始化方法,做了一些初始化工作");
}

Operation result: good again?

Insert picture description here

the reason:

Looking back at the doGet method:

 @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("HelloServlet2的doGet方法");
        // 也可以使用ServletConfig对象
        ServletConfig servletConfig = getServletConfig();
        System.out.println(servletConfig);
//        1. 获取Servlet程序的别名 servlet-name 的值
        System.out.println("HelloServlet程序的别名是:"+servletConfig.getServletName());
//        2. 获取初始化参数 init-param
        System.out.println("HelloServlet程序的初始化参数username的值是:"+servletConfig.getInitParameter("username"));
        System.out.println("HelloServlet程序的初始化参数url的值是:"+servletConfig.getInitParameter("url"));
//        3. 获取 ServletContext 对象
        System.out.println("servletConfig.getServletContext: "+servletConfig.getServletContext());

    }

Here getServletConfig is a method of the GenericServlet class

GenericServlet has only one reference to ServletConfig

GenericServlet:

Insert picture description here

This config is:
Insert picture description here

There is also an init method in GenericServlet, which saves config:
Insert picture description here
when rewriting, when both the subclass and the parent class have init, the subclass will be called when init is called, and the save operation in the parent class will be lost.

Guess you like

Origin blog.csdn.net/weixin_45024585/article/details/108855469