学会本篇博客,再也不会担心面试官问你Servlet了

Servlet 介绍

Servlet API中介绍

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.
简单来说就是:servlet是一个运行在web服务器中的一个小的java项目。servlet接收和响应从web客户端发送的请求,经常通过HTTP,Hyper Text Transfer Protocol
实现这个接口,你可以写一个继承了javax.servlet.GenericServlet的GenericServlet或者是一个继承了javax.servlet.http.HttpServlet.的HttpServlet类。

Servlet中常用的方法

  1. destory();当服务器关闭会执行该方法
  2. getServletConfig();获取Servlet的配置信息
  3. getServletInfo();获取Servlet的信息,比如作者,版本等
  4. init(ServletConfig config);当前端发送请求时会执行该方法进行初始化
  5. service(ServletRequest req,ServletResponse res);通过servlet容器去接收请求和响应

GenericServlet

Defines a generic, protocol-independent servlet. To write an HTTP servlet for use on the Web, extend HttpServlet instead.
GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet may be directly extended by a servlet, although it’s more common to extend a protocol-specific subclass such as HttpServlet.
简单说明:定义一个通用的、与协议无关的servlet。要编写在Web上使用的HTTPservlet,需要继承HttpServlet。
GenericServlet实现了Servlet和ServletConfig接口,GenericServlet可以继承一个Servlet ,尽管扩展一个特定协议的子类,例如:HttpServlet
GenericServlet实现了Servlet接口,所以也拥有Servlet中的方法以及自己特有的方法

GenericServlet特有方法

  1. getervletContext();返回对servlet正在运行的ServletContext的引用。
  2. getServletName();获取该servlet的名字
  3. getInitParameter(String name);返回一个被初始化后的参数,如果该参数不存在返回null
  4. log(String msg);想一个servlet日志文件写入特别的信息,并以Servlet命名为前缀。

HttpServlet

Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:
doGet, if the servlet supports HTTP GET requests
doPost, for HTTP POST requests
doPut, for HTTP PUT requests
doDelete, for HTTP DELETE requests
init and destroy, to manage resources that are held for the life of the servlet
getServletInfo, which the servlet uses to provide information about itself
There’s almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doMethod methods listed above).
简单说明:HttpServlet提供了一个抽象的类,以创建适合于Web站点的HTTP servle,该类必须重写以下的一种方法。
几乎没有理由重写service方法。服务通过将标准HTTP请求分派给每种HTTP请求类型的处理程序方法(上面列出的doMethod方法)来处理标准HTTP请求。
HttpServlet里面都是各种HTTP请求:例如doGet,doPost…

ServletConfig

A servlet configuration object used by a servlet container to pass information to a servlet during initialization.
Servlet容器用于在初始化期间向servlet传递信息的servlet配置对象。

ServletConfig常用方法

  1. getInitParameter(Strign name);返回一个被初始化后的参数,如果该参数不存在返回null
  2. getServletContext();返回对servlet正在运行的ServletContext的引用
  3. getServletName();获取该servlet的名字

Servlet的执行原理

在这里插入图片描述

  1. 通过浏览器发送请求,例如:http://localhost:8080/day02_servlet/quick
  2. DND域名解析
  3. Tomcat解析请求
    上下文路径(day01_servlet)
    资源名称:quick
  4. 从web的根路径/WEB-INF下找到web.xml文件
  5. 读取web.xml文件所获取的url-pattern元素,并判断url-pattern的内容是否是/quick
    如果能找到:指向下步操作
    如果找不到:浏览器会报出404资源文件找不到
  6. 通过/qiuck,找到servlet-mapping映射文件中的servlet-name中的/quick
  7. 根据servlet-name中的/quick找到servlet中的servlet-name中的/quick并找到quick的全路径名
  8. 使用反射创建servlet对象
  9. 把创建的servlet对象,储存到servlet实例缓存池中,提供下一次请求使用。Map(String,Servlet)
  10. 容器创建ServletConfig对象,并调用init方法来完成初始化操作
  11. 容器创建ServletRequest和ServletResponse对象,并调用service方法,处理请求
  12. 在service方法中,对当前请求的客户端做响应

Servlet的生命周期

在这里插入图片描述

public class HelloServlet extends HttpServlet {
    /**
     * 构造器执行顺序
     * 优先执行
     */
    public HelloServlet(){
        System.out.println("构造器执行了");
    }
    /**
     * 前端发送的请求会在service方法中做逻辑处理
     * 一般在开发中不会这样写
     * @param req
     * @param res
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("hello Servlet");
    }

    /**
     * 当web服务器关闭时会执行该方法
     */
    @Override
    public void destroy() {
        System.out.println("销毁容器");
    }

    /**
     * 当前端发送请求时会执行该方法,初始化容器
     * @throws ServletException
     */
    @Override
    public void init() throws ServletException {
        System.out.println("开始初始化容器");
    }
}

当启动tomcat服务器后,在浏览器中访问http://localhost:8080/HelloServlet
输出的数据会在控制台输出,输出数据如下:
在这里插入图片描述
当我们多次访问服务器控制台输出的数据如下:
在这里插入图片描述
当我关闭tomcat服务器时,控制台输出的数据如下:
在这里插入图片描述
从以上来说:第一次访问:init()方法会进行初始化,然后执行service()方法的业务逻辑体,当关闭tomcat服务器后才执行destory()方法;
总之就是:初始化一次;服务多次;销毁一次。

Servlet3.0

好处:通过注解配置Servlet,简化web.xml配置Servlet复杂性,提高开发效率。
测试代码:
在这里插入图片描述
@WebServlet的源码
在这里插入图片描述
该@WebServlet中的源码与web.xml的配置文件相对应,所以当我们在XxxServlet添加上@WebServlet后,就不不需要在Web.xml中
配置映射了,只需要在@WebServlet上配置value属性值,在浏览器中直接访问即可。

Web.xml中的全局变量和局部变量的配置

局部变量基本上是在具体的servlet中配置,比如下面代码设置的字符编码:
在这里插入图片描述
全局变量直接在Web.xml中直接配置,不需要在具体的servlet中配置,比如以下设置的字符编码:
在这里插入图片描述
我在web.xml loginServlet配置中配置了局部变量encode字符编码的配置,当我在浏览器中访问了http://localhost:8080/LoginServlet,全局变量和局部变量都会访问到,如下:
在这里插入图片描述
当在没有配置局部变量访问浏览器http://localhost:8080/RequestTypeServlet能访问到全局变量的编码,而局部变量的编码为null,正如前面所说的
在这里插入图片描述
最后附上servlet可能会遇到的面试题:

  1. Servlet3.0和2.0的区别
    最主要的是3.0增加了注解@WebServlet(…),提高了开发效率
  2. Servlet的生命周期
    初始化一次,执行多次,销毁一次
  3. Servlet的映射地址几种写法/过滤器的过滤规则
    Xxx.do Xxx.action 全路径名匹配
  4. Servlet中如何处理编码问题/过滤器中如何处理编码问题
    在web.xml具体servlet中配置局部变量字符编码,或者在web.xml中配置全局变量字符编码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

double_lifly

点喜欢就是最好的打赏!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值