当前位置:网站首页>Servlet lifecycle
Servlet lifecycle
2022-06-30 04:31:00 【A fat man】
package com.htf.web.controller;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* servlet Life cycle of
* 1. Load one servlet class
* 2. Instantiation
* 3. initialization
* 4. There's a request to come , that call service Method treatment (service Method regardless of the request method , He can handle it )
* 5.service Method according to the request get still post, call doGet( Can only handle get Method request ) perhaps doPost( Can only handle post Method request ) Handle
* 6. The destruction servlet
*
* Under normal circumstances, the life cycle is like this
* Construction method ---->init(ServletConfig config)---->service()---->doGet()
* 1. Only request servlet,servlet Will be instantiated
* 2. Instantiation methods and initialization methods are executed only once in the life cycle
* 3.tomcat When you restart, it will , The destruction servlet
*
* A special case 1
* If servlet A special , Need to be in other servlet Initialization before (tomcat Instantiate at startup servlet), So you can configure loadOnStartup=0
* As we learn later springMVC At the time of the DispatcherServlet, That's what he is servlet
*
* A special case 2
* You need to get it and pass it to servlet When initializing parameters for
Definition servlet Is the initialization parameter of
* <servlet>
<servlet-name>Servlet2</servlet-name>
<servlet-class>com.neuedu.web.controller.Servlet2</servlet-class>
<init-param>
<param-name>ENCODE</param-name>
<param-value>utf-8</param-value>
</init-param>
*</servlet>
* @Override
public void init(ServletConfig config) throws ServletException {
// How to get and pass to servlet The initialization parameters of ?
String value=config.getInitParameter("ENCODE");
System.out.println("encode:"+value);
}
*
* @author htf
*
*/
@WebServlet(value="/Servletlife",loadOnStartup=0)
public class Servletlife extends HttpServlet {
private static final long serialVersionUID = 1L;
public Servletlife() {
System.out.println(" Instantiation Servletlife");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("=====doGet()=====");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("=====doPost()=====");
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("=====service()=====");
//service Method depends on the requested method or get still post, call doGet perhaps doPost
String mothod=request.getMethod();
if("GET".equalsIgnoreCase(mothod)) {
doGet(request, response);
}
if("POST".equalsIgnoreCase(mothod)) {
doPost(request, response);
}
}
@Override
public void destroy() {
System.out.println("=====destroy()=====");
}
@Override
public void init() throws ServletException {
System.out.println("=====init()=====");
}
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("=====init(ServletConfig config)=====");
}
}
边栏推荐
- Machine learning notes
- 【WEBRTC】ADM: rtc_include_internal_audio_device 触发 RTC_DCHECK(adm) 断言
- lego_ Reading and summary of loam code
- oslo_ config. cfg. ConfigFileParseError: Failed to parse /etc/glance/glance-api. Conf: a solution to errors
- FortiGate configures multiple server IPS as link monitor monitoring objects on the same interface
- The same node code will cause duplicate data
- Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)
- Implementation steps of dynamic proxy
- Create thread in callable mode
- Es2018 key summary
猜你喜欢

Bean创建流程 与 lazy-init 延迟加载机制原理
![Tea mall system based on SSM framework [project source code + database script + report]](/img/d9/0a46c0da9839a7186bd3a9ae55f0a5.png)
Tea mall system based on SSM framework [project source code + database script + report]

Intern method of string

Basic knowledge of redis

Introduction to system programming

How the FortiGate firewall rejects a port by using the local in policy policy

lego_ Reading and summary of loam code

Process architecture and process management

Myrpc version 1

How to use div boxes to simulate line triangles
随机推荐
JS proxy
SQL error caused by entity class: Oracle "ora-00904" error: possible case of invalid identifier
FortiGate firewall quick initialization administrator password
BeanFactory创建流程
Clients accessing the daytime service (TCP)
【WEBRTC】ADM: rtc_include_internal_audio_device 触发 RTC_DCHECK(adm) 断言
Myrpc version 5
IO stream, byte stream read / write copy
FortiGate firewall configuration log uploading regularly
FortiGate firewall modifies the default timeout of a session
网络层详解
Qt6 QML Book/Qt Quick 3D/Qt Quick 3D
Myrpc version 2
Myrpc version 4
IO stream, character read / write, copy
Learning about signals
Blocking queue example
oslo_ config. cfg. ConfigFileParseError: Failed to parse /etc/glance/glance-api. Conf: a solution to errors
el-upload上傳文件(手動上傳,自動上傳,上傳進度)
Junior students summarize JS advanced interview questions