当前位置:网站首页>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、
边栏推荐
- [OC foundation framework] - string and date and time >
- Using label template to solve the problem of malicious input by users
- Intel distiller Toolkit - Quantitative implementation 2
- Kratos战神微服务框架(一)
- Intel distiller Toolkit - Quantitative implementation 3
- [oc foundation framework] - < copy object copy >
- Redis之cluster集群
- UML圖記憶技巧
- BMINF的后训练量化实现
- 七层网络体系结构
猜你喜欢
[oc]- < getting started with UI> -- common controls uibutton
Selenium+Pytest自动化测试框架实战(下)
Heap (priority queue) topic
Selenium+pytest automated test framework practice
[three storage methods of graph] just use adjacency matrix to go out
Redis之Geospatial
如何正确截取字符串(例:应用报错信息截取入库操作)
Intel Distiller工具包-量化实现2
[oc foundation framework] - < copy object copy >
Mathematical modeling 2004b question (transmission problem)
随机推荐
Mongodb installation and basic operation
Mathematical modeling 2004b question (transmission problem)
Redis之哨兵模式
[oc]- < getting started with UI> -- learning common controls
Intel distiller Toolkit - Quantitative implementation 1
LeetCode:394. String decoding
[OC]-<UI入门>--常用控件-UIButton
Advanced Computer Network Review(4)——Congestion Control of MPTCP
Pytest's collection use case rules and running specified use cases
五层网络体系结构
[OC-Foundation框架]---【集合数组】
LeetCode:387. The first unique character in the string
Redis cluster
一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?
Intel distiller Toolkit - Quantitative implementation 3
Redis之性能指标、监控方式
Selenium+Pytest自动化测试框架实战(下)
[OC foundation framework] - [set array]
什么是MySQL?MySql的学习之路是怎样的
Five layer network architecture