The java Servlet life cycle (for newcomers)

Someone recently said to me a private letter, I wrote a blog some superficial, did not get to the bottom, and I reply unified look, most of these blog things are conceptual, with popular examples to get you to understand this in the end is how the same principles of technology or knowledge.
So I have added in the back for the newcomers, we are new people to come from, knowing that beginners confused, only the code, do not understand the principle and the meaning of it, for example, notes, you do not learn to understand, beginners I do not know what it is, why you want to use annotations.
If you are a senior Daniel, then do not read, then again, for newcomers.

1, Life Cycle

servlet lifecycle can be divided into four stages:

  1. Class loading procedure
  2. init () initialization process
  3. service () service process, select doGet \ doPost
  4. destroy () the destruction process

Analog Code:

package com.aaa.Servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
* 项目名称:ServletDemo  
* 类描述:("Servlet生命周期")
* 创建人:莫参商 
* 创建时间:2020-1-15 下午4:18:24  
* 修改人:acer     
* @version   
*   
 */
@WebServlet("/ServletDemo02")
public class ServletDemo02 extends HttpServlet{
	
	//初始化,也可以称为开始
	@Override
	public void init() throws ServletException {
		System.out.println("Servlet开始咯... ...init()");
	}
	//service 请求发送   里面有 常用:get post  不常用:put delete 	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("代码被执行... ...");
	}
	@Override
	public void destroy() {
		System.out.println("Servlet销毁咯... ...destroy()");
	}
}

Here Insert Picture Description

2, Servlet is a singleton, thread-safety issues!

First of all we need to know, Servlet is a singleton, which means it can only be instantiated once.
I had the interview, the interviewer asked me, how do you prove Servlet is a singleton pattern?
how to prove?
Wherein the single embodiment, only run once constructors.
So write a constructor to ok

Proved Servlet is a singleton

@WebServlet("/ServletDemo02")
public class ServletDemo02 extends HttpServlet{
	
	public ServletDemo02(){
		System.out.println("啦啦啦,我是ServletDemo02的构造函数!");
	}
	
	//初始化,也可以称为开始
	@Override
	public void init() throws ServletException {
		System.out.println("Servlet开始咯... ...init()");
	}
	//service 请求发送   里面有 常用:get post  不常用:put delete 	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("代码被执行... ...");
	}
	@Override
	public void destroy() {
		System.out.println("Servlet销毁咯... ...destroy()");
	}
}

Here Insert Picture Description
Look result, many visits, but only to create a constructor, so you can prove Servlet is a singleton.
init () method of the servlet own, it has always been performed once, so it can not be evidence of whether a single example.

Develop interview questions:

How to prove Servlet is a singleton pattern?
Wherein the single embodiment, only run once constructors.

Published 14 original articles · won praise 15 · views 4255

Guess you like

Origin blog.csdn.net/qq_41566219/article/details/103991141