Detailed explanation of Servlet interface in javaWeb-2-Tomcat server

table of Contents

1. Servlet interface

1.1 Inheritance system of Servlet interface

2. ServletConfig interface

2.1 ServletConfig interface description

2.2 Three functions of ServletConfig interface

2.3 Code demonstration of ServletConfig interface

2.3.1 Servlet Processor-HttpServletProcessor

2.3.2 Precautions for rewriting the init method

2.3.3 Servlet processor-web.xml configuration file

2.3.4 Servlet processor-front-end HTTP request

2.3.5 Servlet processor-backend HTTP request processing result

3. ServletContext interface

3.1, ServletContext interface description

3.2, the role of the ServletContext object

3.3, the role of the ServletContext object-code demonstration

3.3.1 Servlet Processor-ServletContext Object Function Test-HttpServletProcessorAuto

3.3.2 Servlet processor-ServletContext object function test-web.xml configuration file

3.3.3 Servlet processor-ServletContext object function test-front-end HTTP request

3.3.4 Servlet processor-ServletContext object function test-backend HTTP request processing result

3.3.5 Servlet processor-ServletContext object function test-demonstration of storing data in ServletContext object

3.3.6 After the entire Web project is deployed in the Tomcat server, the dynamic resources and static resources in the server will be stored in the disk

java8 online API : https://www.matools.com/api/java8

1. Servlet interface

1.1 Inheritance system of Servlet interface

2. ServletConfig interface

2.1 ServletConfig interface description

Judging from the name, it is known that the ServletConfig interface is the configuration information of the Servlet program:

(1) Servlet program and ServletConfig object are both created by Tomcat and used by programmers.

(2) The Servlet program is created when it is accessed for the first time by default (no access is not created). When each Servlet program is created, it will create a ServletConfig object corresponding to each other. A Servlet program can only obtain the ServletConfig object corresponding to it, but cannot obtain the ServletConfig object of other Servlet programs.

2.2 Three functions of ServletConfig interface

(1) The alias of the Servlet program (ie the alias of the Servlet program configured in web.xml) can be obtained.

(2) The initialization parameters configured in web.xml can be obtained.

(3) You can get the ServletContext object.

2.3 Code demonstration of ServletConfig interface

2.3.1 Servlet Processor-HttpServletProcessor

package com.wind.servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * 演示ServletConfig接口的作用
 */
public class HttpServletProcessor extends HttpServlet {
    private static final long serialVersionUID = -7511536601794982778L;
    public HttpServletProcessor() {
        System.out.println("1.HttpServletProcessor extends HttpServlet 的无参构造器执行了...");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        System.out.println("2.HttpServletProcessor extends HttpServlet 的init方法执行了...");
        //ServletConfig接口的三大作用:
        //(1) 可以获取Servlet程序的别名(即web.xml的的内容)
        System.out.println("Servlet程序的别名:" + servletConfig.getServletName());
        //(2) 可以获取web.xml的初始化参数的值
        System.out.println("Servlet程序的初始化参数1:" + servletConfig.getInitParameter("username"));
        System.out.println("Servlet程序的初始化参数2:" + servletConfig.getInitParameter("password"));
        //(3) 可以获取ServletContext对象
        System.out.println("Servlet程序的ServletContext对象:" + servletConfig.getServletContext());
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("3.HttpServletProcessor extends HttpServlet, doGet done...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("4.HttpServletProcessor extends HttpServlet, doPost done...");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("5.HttpServletProcessor extends HttpServlet 的destroy方法执行了.");
    }
}

2.3.2 Precautions for rewriting the init method

Note: When rewriting the init method of the Servlet interface, you must write in the function body: super.init(config); The   reason is: the init method in the parent class GenericServlet saves the parameter config, and the subclass cannot save it if it is not called.

2.3.3 Servlet processor-web.xml configuration file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--1.使用servlet标签给Tomcat配置动态servlet程序-->
    <servlet>
        <!--servlet-name标签:给servlet程序起一个别名(一般就是类名)-->
        <servlet-name>HttpServletProcessor</servlet-name>
        <!--servlet-class标签:servlet程序的全类名-->
        <servlet-class>com.wind.servlet.HttpServletProcessor</servlet-class>
        <!--init-param标签:给Servlet程序传入初始化参数-->
        <init-param>
            <param-name>username</param-name>
            <param-value>root</param-value>
        </init-param>
        <init-param>
            <param-name>password</param-name>
            <param-value>root</param-value>
        </init-param>
    </servlet>

    <!--2.使用servlet-mapping标签给动态servlet程序配置访问地址-->
    <servlet-mapping>
        <!--servlet-name标签:告诉Tomcat服务器,我当前配置的访问地址是给哪个servlet程序使用-->
        <servlet-name>HttpServletProcessor</servlet-name>
        <!--url-pattern标签:配置访问地址.
        (1)/            :斜杠表示,在服务器解析的时候,表示访问地址是:http://ip:port/工程路径
        (2)/httpServlet : 表示访问地址是:http://ip:port/工程路径/httpServlet 比如:http://localhost:8080/javaWeb/httpServlet -->
        <url-pattern>/httpServlet</url-pattern>
    </servlet-mapping>
    <!--3.使用b.html访问-->

</web-app>

2.3.4 Servlet processor-front-end HTTP request

2.3.5 Servlet processor-backend HTTP request processing result

3. ServletContext interface

3.1, ServletContext interface description

(1) The ServletContext interface represents the Servlet context object.

(2) A web project has only one ServletContext object instance.

(3) ServletContext is created when the web project is started, and destroyed when the web project is stopped.

(4) The ServletContext object is a domain object. What is a domain object? The domain object is: the object that accesses data like a Map is called the domain object, the domain  refers to the operation scope of accessing the data, and the domain of the ServletContext is the entire web project.

3.2, the role of the ServletContext object

(1) Get the value in the context parameter tag configured in web.xml.

(2) Get the path of the current project, format: /project path, which is the content of the Application context in the Deployment in Edit Configurations (that is, the content after 8080 in the address, before the specific opened page).

(3) Obtain the absolute path of the project on the hard disk after deployment.

(4) Access data like Map.

3.3, the role of the ServletContext object-code demonstration

3.3.1 Servlet Processor-ServletContext Object Function Test-HttpServletProcessorAuto

package com.wind.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ServletContext 测试
 */
public class HttpServletProcessorAuto extends HttpServlet {

    private static final long serialVersionUID = 2050438983192725273L;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    /**
     * 默认执行的是doGet方法,故只重写doGet方法
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //GenericServlet类中有 public ServletConfig getServletConfig()方法,返回 this.config
        ServletContext servletContext = getServletConfig().getServletContext();

        //ServletContext作用 1.获取web.xml中配置的上下文参数<context-param>标签中的值
        System.out.println("context-param参数的username值是:" + servletContext.getInitParameter("username"));
        /* 运行结果:context-param参数的username值是:username-root */
        System.out.println("context-param参数的password值是:" + servletContext.getInitParameter("password"));
        /* 运行结果:context-param参数的password值是:password-root */

        //ServletContext作用 2.获取当前工程的路径
        System.out.println("当前工程路径:" + servletContext.getContextPath());
        /* 运行结果:当前工程路径:/javaWeb */

        //ServletContext作用 3.获取web工程部署后在硬盘上的绝对路径
        /* /斜杠(默认路径)对应IDEA代码的web目录 */
        System.out.println("工程部署后的绝对路径是:" + servletContext.getRealPath("/"));
        /* 运行结果:工程部署后的绝对路径是:/Users/cmm/codes/spring5-txdemo3-javaWeb/out/artifacts/javaWeb_war_exploded/ */

        //另:在web目录下创建一个css文件夹
        System.out.println("工程下css目录的绝对路径是:" + servletContext.getRealPath("/css"));
        /* 运行结果:工程下css目录的绝对路径是:/Users/cmm/codes/spring5-txdemo3-javaWeb/out/artifacts/javaWeb_war_exploded/css */
        //在web目录下创建一个img文件夹,里面放1.gif文件
        System.out.println("工程下img目录1.jpg的绝对路径是:" + servletContext.getRealPath("/img/1.jpg"));
        /* 输出:工程下img目录1.gif的绝对路径是:/Users/cmm/codes/spring5-txdemo3-javaWeb/out/artifacts/javaWeb_war_exploded/img/1.jpg */
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("3.HttpServletProcessorAuto extends HttpServlet, doGet done...");
    }
}

3.3.2 Servlet processor-ServletContext object function test-web.xml configuration file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--<context-param>标签中是web工程的上下文参数,属于整个web工程-->
    <!--<context-param>标签可以有多个,但是,全部都需要写在第一个<servlet>标签之上!!!-->
    <context-param>
        <param-name>username</param-name>
        <param-value>username-root</param-value>
    </context-param>
    <context-param>
        <param-name>password</param-name>
        <param-value>password-root</param-value>
    </context-param>
    <!--<context-param>标签下方,才能够是servlet类对应的<servlet标签>-->

    <!--1.使用servlet标签给Tomcat配置动态servlet程序-->
    <servlet>
        <servlet-name>HttpServletProcessorAuto</servlet-name>
        <servlet-class>com.wind.servlet.HttpServletProcessorAuto</servlet-class>
    </servlet>

    <!--2.使用servlet-mapping标签给动态servlet程序配置访问地址-->
    <servlet-mapping>
        <servlet-name>HttpServletProcessorAuto</servlet-name>
        <url-pattern>/httpServletAuto</url-pattern>
    </servlet-mapping>
    <!--3.使用b.html访问-->

</web-app>

3.3.3 Servlet processor-ServletContext object function test-front-end HTTP request

3.3.4 Servlet processor-ServletContext object function test-backend HTTP request processing result

3.3.5 Servlet processor-ServletContext object function test-demonstration of storing data in ServletContext object

(1) Servlet processor-HttpServletProcessorAuto2

public class HttpServletProcessorAuto2 extends HttpServlet {

    private static final long serialVersionUID = -1924341414254477696L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //GenericServlet类中有 public ServletConfig getServletConfig()方法,返回 this.config
        ServletContext servletContext = getServletConfig().getServletContext();

        //向servletContext对象中设置值
        System.out.println("HttpServletProcessorAuto2=servletContext对象,key1=:" + servletContext.getAttribute("key1"));
        System.out.println("HttpServletProcessorAuto2=servletContext对象hashcode=:" + servletContext);
    }
}

(2) Servlet processor-HttpServletProcessorAuto

public class HttpServletProcessorAuto extends HttpServlet {

    private static final long serialVersionUID = 2050438983192725273L;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    /**
     * 默认执行的是doGet方法,故只重写doGet方法
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //GenericServlet类中有 public ServletConfig getServletConfig()方法,返回 this.config
        ServletContext servletContext = getServletConfig().getServletContext();

        //向servletContext对象中设置值
        System.out.println("HttpServletProcessorAuto=servletContext对象设置值之前,key1=:" + servletContext.getAttribute("key1"));
        servletContext.setAttribute("key1", "value1");
        System.out.println("HttpServletProcessorAuto=servletContext对象设置值之后,key1=:" + servletContext.getAttribute("key1"));
        System.out.println("HttpServletProcessorAuto=servletContext对象hashcode=:" + servletContext);
    }
}

(3) web.xml configuration file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--<context-param>标签中是web工程的上下文参数,属于整个web工程-->
    <!--<context-param>标签可以有多个,但是,全部都需要写在第一个<servlet>标签之上!!!-->
    <context-param>
        <param-name>username</param-name>
        <param-value>username-root</param-value>
    </context-param>
    <context-param>
        <param-name>password</param-name>
        <param-value>password-root</param-value>
    </context-param>
    <!--<context-param>标签下方,才能够是servlet类对应的<servlet标签>-->

    <!--1.使用servlet标签给Tomcat配置动态servlet程序-->
    <servlet>
        <servlet-name>HttpServletProcessorAuto</servlet-name>
        <servlet-class>com.wind.servlet.HttpServletProcessorAuto</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>HttpServletProcessorAuto2</servlet-name>
        <servlet-class>com.wind.servlet.HttpServletProcessorAuto2</servlet-class>
    </servlet>

    <!--2.使用servlet-mapping标签给动态servlet程序配置访问地址-->
    <servlet-mapping>
        <servlet-name>HttpServletProcessorAuto</servlet-name>
        <url-pattern>/httpServletAuto</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>HttpServletProcessorAuto2</servlet-name>
        <url-pattern>/httpServletAuto2</url-pattern>
    </servlet-mapping>
    <!--3.使用b.html访问-->
</web-app>

(4) Operation results

3.3.6 After the entire Web project is deployed in the Tomcat server, the dynamic resources and static resources in the server will be stored in the disk

(1) After the entire Web project is deployed in the Tomcat server, the dynamic resources and static resources in the server will be stored in the disk, and the directory is as follows.

(2) And it can be directly opened in the browser to view various resources deployed in it.

(3) So, others can access the various resources deployed by the developer in the Tomcat server in the browser. The access process is roughly as follows.

 

 

Guess you like

Origin blog.csdn.net/cmm0401/article/details/111414121