Servlet生命周期

方法名作用
init()初始化
service()处理客户端请求
desdroy()销毁

最后被gc回收

init()

init()方法只在第一次创建的时候被调用一次
    //如果有相关配置可以调用这个init进行加载
    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        this.init();
    }

    //常用init方法
    public void init() throws ServletException {
    //初始化代码
    }

service()

service() 方法是执行实际任务的主要方法,用来处理客户端的请求并返回响应,内部根据接口类型调用一下源码中的各种请求类型。

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        long lastModified;
        if(method.equals("GET")) {
            lastModified = this.getLastModified(req);
            if(lastModified == -1L) {
                this.doGet(req, resp);
            } else {
                long ifModifiedSince = req.getDateHeader("If-Modified-Since");
                if(ifModifiedSince < lastModified) {
                    this.maybeSetLastModified(resp, lastModified);
                    this.doGet(req, resp);
                } else {
                    resp.setStatus(304);
                }
            }
        } else if(method.equals("HEAD")) {
            lastModified = this.getLastModified(req);
            this.maybeSetLastModified(resp, lastModified);
            this.doHead(req, resp);
        } else if(method.equals("POST")) {
            this.doPost(req, resp);
        } else if(method.equals("PUT")) {
            this.doPut(req, resp);
        } else if(method.equals("DELETE")) {
            this.doDelete(req, resp);
        } else if(method.equals("OPTIONS")) {
            this.doOptions(req, resp);
        } else if(method.equals("TRACE")) {
            this.doTrace(req, resp);
        } else {
            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[]{method};
            errMsg = MessageFormat.format(errMsg, errArgs);
            resp.sendError(501, errMsg);
        }

    }

一般操作的时候,不直接对service()进行操作,而是根据约定,直接对调用的方法类型进行逻辑编写,最常用的还是getpost

doGet

一般用来获取内容,也可以提交少量参数
public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
    throws ServletException, IOException {
    // 逻辑代码
}

doPost

一般用来提交内容,相对get较安全
public void doPost(HttpServletRequest request,
                   HttpServletResponse response)
    throws ServletException, IOException {
    //逻辑代码
}

doGet和doPost的区别

get请求因为是明文,入参直接加在url后面,所以被限制在1024字节

post请求没有明文规定数据大小,现在一般用json格式的数据作为附件进行传送的,但是如果你上传比服务器容量还大的数据,一样会被群殴

destroy()

destroy方法和init方法对应,一个初始化,一个销毁。
会默认在GC之前关闭一切活动, 关闭数据库连接、停止后台线程、把 Cookie 列表或点击计数器写入到磁盘,并执行其他类似的清理活动等等,然后servlet 对象被标记为垃圾回收。

    @Override
    public void destroy() {
        super.destroy();
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值