【Servlet04】Servlet继承结构

可以通过Idea查看Servlet的继承结构:

1 ServletConfig接口

API介绍:

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

涉及到的方法:

1.1 getInitParameter方法

声明:

java.lang.String getInitParameter(java.lang.String name);

使用这个方法需要先配置web.xml增加<init-param>标签设置初始化参数:

<servlet>
    <servlet-name>helloServelt</servlet-name>
    <servlet-class>com.jackqiang.HelloServlet</servlet-class>
    <init-param>
        <param-name>address</param-name>
        <param-value>Earth</param-value>
    </init-param>
    <init-param>
        <param-name>country</param-name>
        <param-value>China</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
  • 注意:<init-param>标签一定要在<load-on-startup标签之前,顺序不能搞反;

  • <param-name>是键,<param-value>是值,getInitParameter方法填入键返回值,如果键不存在则返回空;

修改doGet方法进行调用:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("execute doGet......");
    String address = getInitParameter("address");
    String country = getInitParameter("country");
    System.out.println("address = " + address);
    System.out.println("country = " + country);
}

重启服务器,访问Servlet查看结果:

1.2 getInitParameterNames方法

声明:

 java.util.Enumeration getInitParameterNames();

该方法用一个字符串对象枚举变量的形式返回所有初始化参数的键,使用:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("execute doGet......");
    Enumeration<String> initParameterNames = getInitParameterNames();
    while (initParameterNames.hasMoreElements()){
        String s = initParameterNames.nextElement();
        System.out.println(s);
    }
}

重启服务器,访问Servlet查看结果:

1.3 getServletName方法

声明:

java.lang.String getServletName()

该方法返回<servlet-name>标签中的值,使用:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("execute doGet......");
    String servletName = getServletName();
    System.out.println("servletName = " + servletName);
}

重启服务器,访问Servlet查看结果:

1.4 getServletContext方法

声明:

ServletContext getServletContext() 

该方法返会一个ServletContext对象,关于ServletContext对象比较重要,后续会详细展开介绍。

2 Servlet接口

API介绍:

Defines methods that all servlets must implement.

A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.

To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.

This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:

  1. The servlet is constructed, then initialized with the init method.

  2. Any calls from clients to the service method are handled.

  3. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.

In addition to the life-cycle methods, this interface provides the getServletConfig method, which the servlet can use to get any startup information, and the getServletInfo method, which allows the servlet to return basic information about itself, such as author, version, and copyright.

涉及到的方法:

2.1 getServletConfig和getServletInfo方法

这两个方法不怎么使用,简单介绍一下:

  • ServletConfig getServletConfig():返回一个 ServletConfig实现类对象,包含了servlet的初始化和起始参数,得到了对象以后又可以调用ServletConfig的方法;

  • java.lang.String getServletInfo():返回有关Servlet的信息,例如作者、版本、版权,这些信息写在Servlet的类注释中;

2.2 Servlet生命周期方法

Servlet生命周期设计到三个方法:

  • void init(ServletConfig config)

  • void service(ServletRequest req, ServletResponse res)

  • void destroy()

这三个方法需要重点介绍。

Servlet的生命周期分为三个阶段,初始化阶段、处理请求阶段、销毁阶段

  • 初始化阶段:只会执行一次,在初始化阶段会自动调用 void init(ServletConfig config)方法;

    • Servlet类初始化的时间:默认情况下是第一次访问时,或者是服务器启动时;

  • 处理请求阶段:持续性的,在浏览器发送请求服务器接收到请求后,就进入了处理请求阶段,此时会自动调用void service(ServletRequest req, ServletResponse res)方法;

    • 注意只会自动调用service方法,doXxx()等处理请求方法是service方法根据不同的的逻辑根据情况,手动调用的;

  • 销毁阶段:一次性的,在关闭或重启服务器时,对象会被销毁,此时会自动调用void destroy()方法;

在Servlet中重写这三个方法进行测试:

Servlet:

package com.jackqiang;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

public class HelloServlet extends HttpServlet {
    public HelloServlet() {
        System.out.println("调用了无参构造......");
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("execute service......");
    }

    @Override
    public void destroy() {
        System.out.println("execute destroy......");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("execute init......");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("execute doGet......");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("execute doPost......");
    }
}

重启服务器,访问Servlet查看结果:

可以看到此时没有执行doGet和doPost方法,正如上述,这些doXxx()方法是service方法根据不同情况去手动执行的:

所以如果需要执行doXxx()方法,需要加上super.service(req,res);

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    System.out.println("execute service......");
    super.service(req,res);
}

3 GenericServlet类和HttpServlet类

  • GenericServlet类:实现了Servlet接口和ServletConfig接口的抽象方法;

  • HttpServlet:包含了doXxx()方法和service方法,我们的Servelt继承这个类的时候需要重写其中的方法;

版权声明:
作者:jackqiang
链接:http://www.jackqiang.com/javaweb/server/servlet/2119/servlet_inherit/
来源:JackQiang's
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>
文章目录
关闭
目 录