0
点赞
收藏
分享

微信扫一扫

还记得JavaWeb中的组件Servlet吗?面试会问

文章目录


一、前言

  • 了解工程目录结构
  • 学会创建servlet程序
  • 知道servlet声明周期、继承体系
  • ServletConfig、ServletContext类的讲解

废话不多说,满满的干货,赶快来看看吧~

二、工程目录结构

在这里插入图片描述

三、servlet简介

什么是servlet?

  • servlet是JavaEE规范之一,规范就是接口
  • servlet是JavaWeb三大组件之一。三大组件分别是servlet程序、filter过滤器、listener监听器
  • servlet是运行在服务器上的一个java小程序,它可以接收客户端发送过来的请求,并响应数据给客户端

四、实现servlet程序

其实操作很简单,编写一个类去实现servlet接口:

public class FirstServlet implements Servlet{
    public FirstServlet() {
        System.out.println("我是构造器");
    }
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("我是初始化方法");
    }
    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("service方式执行了");
    }
    @Override
    public String getServletInfo() {
        return null;
    }
    @Override
    public void destroy() {
        System.out.println("我被摧毁了~");
    }
}

然后在web.xml配置文件中配置servlet程序的访问地址:

<?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">
    <servlet>
        <servlet-name>FirstServlet</servlet-name>
        <servlet-class>com.cabbage.FirstServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FirstServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

需要注意的是:

标签作用
<servlet>给Tomcat配置servlet程序
<servlet-name>给servlet程序起一个别名
<servlet-class>servlet程序的全类名
<servlet-mapping>给servlet程序配置访问地址
<servlet-name>是告诉服务器,当前配置的地址给哪个servlet程序使用
<url-pattern>配置访问地址
/斜杠在服务器解析的时候,表示地址为http://ip:port/工程路径
/hello表示地址为http://ip:port/工程路径/hello

在这里插入图片描述

五、servlet的生命周期

运行上面的代码,就可以得到以下结果:
在这里插入图片描述

  • 先执行servlet构造器方法
  • 接着执行init初始化方法
  • 执行service方法,此方法每次访问都会调用
  • 最后再工程停止的时候执行destroy方法

六、servlet类的继承体系

在这里插入图片描述

七、ServletConfig类:

ServletConfig类的三大作用:

  • 可以获取 Servlet 程序的别名 servlet-name 的值
  • 获取初始化参数 init-param
  • 获取 ServletContext 对象

为了方便测试,我们在web.xml配置文件中添加一些内容:

    <servlet>
        <servlet-name>FirstServlet</servlet-name>
        <servlet-class>com.cabbage.FirstServlet</servlet-class>
        <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>
    <servlet-mapping>
        <servlet-name>FirstServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

写一个java代码用来测试:

public class FirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig config = getServletConfig();
        String name = config.getServletName();
        String username = config.getInitParameter("username");
        String password = config.getInitParameter("password");
        ServletContext servletContext = config.getServletContext();
        System.out.println(name);
        System.out.println(username);
        System.out.println(password);
        System.out.println(servletContext);
    }
}

运行结果无误:
在这里插入图片描述

八、ServletContext 类

什么是ServletContext类?

  • ServletContext 是一个接口,它表示 Servlet 上下文对象
  • 一个 web 工程,只有一个 ServletContext 对象实例。
  • ServletContext 对象是一个域对象。
  • ServletContext 是在 web 工程部署启动的时候创建。在 web 工程停止的时候销毁。

ServletContext类的四个作用:

  • 获取 web.xml 中配置的上下文参数 context-param
  • 获取当前的工程路径,格式: /工程路径
  • 获取工程部署后在服务器硬盘上的绝对路径
  • 像 Map 一样存取数据

在web.xml配置文件中添加全局配置:

    <context-param>
        <param-name>username</param-name>
        <param-value>root</param-value>
    </context-param>
    <context-param>
        <param-name>password</param-name>
        <param-value>root</param-value>
    </context-param>

写一个java代码用来测试:

public class FirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = getServletContext();
        String contextPath = context.getContextPath();
        String username = context.getInitParameter("username");
        String password = context.getInitParameter("password");
        String realPath = context.getRealPath("/");
        System.out.println(contextPath);
        System.out.println(username);
        System.out.println(password);
        System.out.println(realPath);
    }
}

运行结果完全正确:
在这里插入图片描述
接下来,我们新建一个servlet程序用来测试同一个对象:

public class Servlet2 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletContext();
        context.setAttribute("key1","value1");
        System.out.println(context);
    }
}
public class FirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = getServletContext();
        Object key1 = context.getAttribute("key1");
        System.out.println(key1);
        System.out.println(context);
    }
}

运行的结果:
在这里插入图片描述

九、总结

在这里插入图片描述

举报

相关推荐

0 条评论