javaWeb-2-Tomcat服务器中的Servlet接口详解

目录

1、Servlet 接口

1.1、Servlet 接口的继承体系

2、ServletConfig 接口

2.1 ServletConfig 接口描述

2.2 ServletConfig 接口的三大作用

2.3 ServletConfig 接口的代码演示

2.3.1 Servlet处理器-HttpServletProcessor

2.3.2 重写init方法的注意事项

2.3.3 Servlet处理器-web.xml 配置文件

2.3.4 Servlet处理器-前端HTTP请求

2.3.5 Servlet处理器-后端HTTP请求处理结果

3、ServletContext 接口

3.1、ServletContext 接口描述

3.2、ServletContext 对象的作用

3.3、ServletContext 对象的作用-代码演示

3.3.1 Servlet处理器-ServletContext对象功能测试-HttpServletProcessorAuto

3.3.2 Servlet处理器-ServletContext对象功能测试-web.xml 配置文件

3.3.3 Servlet处理器-ServletContext对象功能测试-前端HTTP请求

3.3.4 Servlet处理器-ServletContext对象功能测试-后端HTTP请求处理结果

3.3.5 Servlet处理器-ServletContext对象功能测试-向ServletContext对象中存储数据演示

3.3.6 整个Web项目部署在Tomcat服务器中后,服务器中的动态资源和静态资源会存在磁盘中

java8在线APIhttps://www.matools.com/api/java8

1、Servlet 接口

1.1、Servlet 接口的继承体系

2、ServletConfig 接口

2.1 ServletConfig 接口描述

从名字来看,得知 ServletConfig 接口中是 Servlet 程序的配置信息:

(1)Servlet程序和ServletConfig对象都是由Tomcat负责创建,编程人员负责使用。

(2)Servlet程序默认是第一次访问时创建(不访问不创建),每个Servlet程序创建时会对应的创建ServletConfig对象,二者相互对应。某个Servlet程序只可以获得与它对应的ServletConfig对象,无法获得别的Servlet程序的ServletConfig对象。

2.2 ServletConfig 接口的三大作用

(1)可以获取Servlet程序的别名(即web.xml中配置的Servlet程序的别名)。

(2)可以获取web.xml中配置的初始化参数。

(3)可以获取ServletContext对象。

2.3 ServletConfig 接口的代码演示

2.3.1 Servlet处理器-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 重写init方法的注意事项

注意:重写 Servlet 接口的 init 方法时,必须要在函数体内写:super.init(config);  原因是:父类GenericServlet中的init方法将参数config保存起来,子类若不调用则无法保存。

2.3.3 Servlet处理器-web.xml 配置文件

<?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处理器-前端HTTP请求

2.3.5 Servlet处理器-后端HTTP请求处理结果

3、ServletContext 接口

3.1、ServletContext 接口描述

(1)ServletContext接口表示Servlet上下文对象。

(2)一个web工程只有一个ServletContext对象实例。

(3)ServletContext是在web工程启动时创建,在web工程停止时销毁。

(4)ServletContext对象是一个域对象。什么是域对象呢?域对象就是:像Map一样存取数据的对象称为域对象,域 指的是存取数据的操作范围,ServletContext的域就是整个web工程。

3.2、ServletContext 对象的作用

(1)获取web.xml 中配置的上下文参数标签中的值。

(2)获取当前工程的路径,格式:/工程路径,也就是 Edit Configurations 中 Deployment 中的 Application context 的内容(即地址中8080之后,具体的打开的页面之前的内容)。

(3)获取工程部署后在硬盘上的绝对路径。

(4)像Map一样存取数据。

3.3、ServletContext 对象的作用-代码演示

3.3.1 Servlet处理器-ServletContext对象功能测试-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处理器-ServletContext对象功能测试-web.xml 配置文件

<?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处理器-ServletContext对象功能测试-前端HTTP请求

3.3.4 Servlet处理器-ServletContext对象功能测试-后端HTTP请求处理结果

3.3.5 Servlet处理器-ServletContext对象功能测试-向ServletContext对象中存储数据演示

(1)Servlet处理器-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处理器-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 配置文件

<?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)运行结果

3.3.6 整个Web项目部署在Tomcat服务器中后,服务器中的动态资源和静态资源会存在磁盘中

(1)整个Web项目部署在Tomcat服务器中后,服务器中的动态资源和静态资源会存在磁盘中,目录如下。

(2)并且在浏览器中可以直接打开,查看其中部署的各种资源。

(3)于是乎,别人就可以在浏览器中访问开发人员在Tomcat服务器中部署的各种资源了,访问流程大致如下。

猜你喜欢

转载自blog.csdn.net/cmm0401/article/details/111414121
今日推荐