当前位置:网站首页>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、
边栏推荐
- Post training quantification of bminf
- 不同的数据驱动代码执行相同的测试场景
- UML diagram memory skills
- LeetCode:39. Combined sum
- Intel distiller Toolkit - Quantitative implementation 3
- 数字人主播618手语带货,便捷2780万名听障人士
- Parameterization of postman
- [OC]-<UI入门>--常用控件-UIButton
- Selenium+pytest automated test framework practice (Part 2)
- Improved deep embedded clustering with local structure preservation (Idec)
猜你喜欢
多元聚类分析
[oc]- < getting started with UI> -- learning common controls
I-BERT
IDS' deletion policy
Parameterization of postman
Intel Distiller工具包-量化实现1
[OC-Foundation框架]--<Copy对象复制>
Advanced Computer Network Review(4)——Congestion Control of MPTCP
QML control type: Popup
Advance Computer Network Review(1)——FatTree
随机推荐
Advance Computer Network Review(1)——FatTree
QML type: locale, date
An article takes you to understand the working principle of selenium in detail
LeetCode:34. Find the first and last positions of elements in a sorted array
Improved deep embedded clustering with local structure preservation (Idec)
[three storage methods of graph] just use adjacency matrix to go out
QDialog
Li Kou daily question 1 (2)
Advance Computer Network Review(1)——FatTree
Redis core configuration
Advanced Computer Network Review(5)——COPE
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
go-redis之初始化连接
LeetCode:39. Combined sum
Leetcode刷题题解2.1.1
LeetCode:26. Remove duplicates from an ordered array
Mise en œuvre de la quantification post - formation du bminf
[oc foundation framework] - < copy object copy >
LeetCode:236. The nearest common ancestor of binary tree
Selenium+Pytest自动化测试框架实战