当前位置:网站首页>Servlet learning diary 8 - servlet life cycle and thread safety
Servlet learning diary 8 - servlet life cycle and thread safety
2022-07-06 09:15:00 【herb. dr】
Catalog
One 、 There are four stages in the life cycle
2.2 How to ensure thread safety
One 、 There are four stages in the life cycle
1.1 Instantiation
When the user first accesses Servlet when , Called by container Servlet The constructor of creates a concrete Servlet object . You can also create instances as soon as the container starts . Use the following code to set Servlet Whether to create... When the server starts .
<load-on-starup>1</load-on-starup>
Be careful : Only once
1.2 initialization
In the initialization phase ,init() Method will be called . This method is javax.servlet.Servlet Interface . among , Method with a ServletConfig Object of type as parameter .
Be careful :init Method is executed only once
1.3 service
When the client has a request , The container will request ServletRequest And response ServletResponse Object turn Servlet, In the form of parameters to service Method .
This method executes multiple times
1.4 The destruction
When Servlet Containers (Tomcat) Stopping or restarting will lead to | Destroy from Servlet Object and call destroy Method .
destroy Method execution one Time

1.5 Code

package com.ha.servlet;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class LifeServlet implements Servlet{
public LifeServlet(){
System.out.println("1、 Instantiation ");
}
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("2、 initialization ");
}
public ServletConfig getServletConfig() {
return null;
}
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("3、 Receiving request , In response to the results ");
}
public String getServletInfo() {
return null;
}
public void destroy() {
System.out.println("4、 The destruction ");
}
}


perform shutdown.bat, This tomcat The destroyed content will be printed in a flash in the window of
Two 、 characteristic
2.1 Thread safety problem
Servlet After the visit , Will execute instantiation operation , Create a Servlet object . And we Tomcat The container can access the same server concurrently by multiple threads at the same time Servlet, If you modify a member variable in a method , There will be thread safety problems .
Code interpretation , More than two people visit at the same time :
package com.ha.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class SafeServlet extends HttpServlet {
private String message = "";
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// hypothesis 1、 Receiving parameters
//2、 Call business logic Get login results
message = " Login successful ";// Login failed !
PrintWriter printWriter = resp.getWriter();
printWriter.println(message);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
2.2 How to ensure thread safety
1、synchronized
Put the code with thread safety problems into the synchronization code block
2、 Realization singleThreadModel Interface
servlet Realization singleThreadModel After the interface , Every thread creates servlet example , In this way, each client request does not have the problem of sharing resources , however servlet Too inefficient to respond to client requests , So it has been eliminated .
3、 Use local variables as much as possible
2.3 Realization
1、 If locks are used

The efficiency of the code will be very low , Only one person is allowed to access
2、

3、
边栏推荐
- Heap (priority queue) topic
- AcWing 2456. Notepad
- 七层网络体系结构
- What is an R-value reference and what is the difference between it and an l-value?
- Pytest之收集用例规则与运行指定用例
- KDD 2022 paper collection (under continuous update)
- 不同的数据驱动代码执行相同的测试场景
- Selenium+pytest automated test framework practice
- QDialog
- [OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
猜你喜欢

Simclr: comparative learning in NLP

Kratos ares microservice framework (II)

UML圖記憶技巧

IJCAI2022论文合集(持续更新中)

Persistence practice of redis (Linux version)

postman之参数化详解

LeetCode:221. Largest Square

An article takes you to understand the working principle of selenium in detail
![[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth](/img/10/c0545cb34621ad4c6fdb5d26b495ee.jpg)
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth

BMINF的後訓練量化實現
随机推荐
Advanced Computer Network Review(3)——BBR
Kratos战神微服务框架(三)
[shell script] use menu commands to build scripts for creating folders in the cluster
Leetcode: Sword finger offer 48 The longest substring without repeated characters
ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
QML control type: menu
AcWing 2456. Notepad
Mongodb installation and basic operation
五层网络体系结构
go-redis之初始化連接
[OC]-<UI入门>--常用控件-UIButton
Redis之cluster集群
A convolution substitution of attention mechanism
Sentinel mode of redis
CUDA realizes focal_ loss
Intel Distiller工具包-量化实现2
Using label template to solve the problem of malicious input by users
Redis之核心配置
postman之参数化详解
使用标签模板解决用户恶意输入的问题