servlet多线程安全问题
Servelet多线程安全问题
原因
一个servlet被实例化一次,当有多个用户访问时,多个线程会访问同一个实例,实例变量就会被不同的用户修改。
简单的案例
新建servlet,访问http://localhost:8080/serverlet/servlert1?u=aaa,即可看到aaa输出

public class servlert1 extends HttpServlet {
String username; //实例变量username
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
username=request.getParameter("u");
//睡眠5s,模拟多并发访问
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//输出u接收到的参数
response.getOutputStream().write((username).getBytes());
}
}
A访问http://localhost:8080/serverlet/servlert1?u=aaa
B立马访问http://localhost:8080/serverlet/servlert1?u=bbb
A输出bbb(A的数据被B修改了),B也输出BBB

解决
避免实例变量
public class servlert1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username;
username=request.getParameter("u");
//睡眠5s,模拟多并发访问
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//输出u接收到的参数
response.getOutputStream().write((username).getBytes());
}
}
Servlet 实现SingleThreadModel接口
public class servlert1 extends HttpServlet implements SingleThreadModel{
String username; //实例变量username
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
username=request.getParameter("u");
//睡眠5s,模拟多并发访问
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//输出u接收到的参数
response.getOutputStream().write((username).getBytes());
}
}
同步化
public class servlert1 extends HttpServlet {
String username; //实例变量username
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//同步访问
synchronized (this) {
username=request.getParameter("u");
//睡眠5s,模拟多并发访问
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//输出u接收到的参数
response.getOutputStream().write((username).getBytes());
}
}
}

servlet多线程安全问题的更多相关文章
- javaweb回顾第六篇谈一谈Servlet线程安全问题
前言:前面说了很多关于Servlet的一些基础知识,这一篇主要说一下关于Servlet的线程安全问题. 1:多线程的Servlet模型 要想弄清Servlet线程安全我们必须先要明白Servlet实例 ...
- Servlet线程安全问题
Servlet采用单实例多线程方式运行,因此是线程不安全的.默认情况下,非分布式系统,Servlet容器只会维护一个Servlet的实例,当多个请求到达同一个Servlet时,Servlet容器会启动 ...
- (2.1)servlet线程安全问题
本文参考链接:http://www.yesky.com/334/1951334.shtml 摘 要:介绍了Servlet多线程机制,通过一个实例并结合Java 的内存模型说明引起Servlet线程不安 ...
- servlet多线程
一,servlet容器如何同时处理多个请求. Servlet采用多线程来处理多个请求同时访问,Servelet容器维护了一个线程池来服务请求.线程池实际上是等待执行代码的一组线程叫做工作者线程(Wor ...
- servlet 多线程
servlet在服务器中只有一个实例,那么它响应请求的方式应该是多线程. 一,servlet容器如何同时处理多个请求. Servlet采用多线程来处理多个请求同时访问,Servelet容器维护了一个线 ...
- Servlet的线程是不是共享同一个requset对象及servlet多线程
servlet多线程 一,servlet容器如何同时处理多个请求. Servlet采用多线程来处理多个请求同时访问,Servelet容器维护了一个线程池来服务请求.线程池实际上是等待执行代码的一组 ...
- Servlet线程安全问题(转载)
转载地址:https://www.cnblogs.com/LipeiNet/p/5699944.html 前言:前面说了很多关于Servlet的一些基础知识,这一篇主要说一下关于Servlet的线程安 ...
- JAVAEE_Servlet_18_关于Servlet线程安全问题
关于Servlet线程安全问题 Servlet线程安全 Servlet 是单实例多线程的环境下运行的. 在服务器运行期间,一个Servlet接口实现类,只能创建一个实例对象(一个进程(Servlet接 ...
- java基础知识回顾之java Thread类学习(四)--java多线程安全问题(锁)
上一节售票系统中我们发现,打印出了错票,0,-1,出现了多线程安全问题.我们分析为什么会发生多线程安全问题? 看下面线程的主要代码: @Override public void run() { // ...
随机推荐
- C++ DLL debug版本在其他PC上缺少依赖的处理方式
1.正常情况提供给其他人的都是Release版本DLL 2.在需要提供Debug版本时,目标机器上可能会缺少环境,或者和生成DLL的环境不匹配导致DLL无法加载,提示DLL无法找到. 3.使用DLL依 ...
- Zookeeper 安装及集群配置注意点
Zookeeper在ubuntu下安装及集群搭建,关于集群搭建,网上很多文章 可以参考:https://www.ibm.com/developerworks/cn/opensource/os-cn-z ...
- 关于ECharts甘特图的实现
对于使用ECharts图表的步骤,每种图表都是一致的,相信大家也都了解 此处只分享甘特图的option,代码如下: option: { title: { text: '项目实施进度表', left: ...
- 实现Vue的双向绑定
一.概述 之前有讲到过vue实现整体的整体流程,讲到过数据的响应式,是通过Object.defineProperity来实现的,当时只是举了一个小小的例子,那么再真正的vue框架里是如何实现数据的双向 ...
- 什么是N+1查询?
在Session的缓存中存放的是相互关联的对象图.默认情况下,当Hibernate从数据库中加载Customer对象时,会同时加载所有关联的Order对象.以Customer和Order类为例,假定O ...
- 【Zookeeper】实现哨兵机制(选举策略)
一.Master选举使用场景及结构 二.代码实现 2.1 Maven依赖信息 2.2 IndexController 2.3 MyApplicationRunner 2.4 ElectionMaste ...
- Linux下 expect 使用详解与实例
一.概述 我们通过Shell可以实现简单的控制流功能,如:循环.判断等.但是对于需要交互的场合则必须通过人工来干预,有时候我们可能会需要实现和交互程序如telnet服务器等进行交互的功能.而Expec ...
- java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present
@Controller@ComponentScan@Configuration@EnableScheduling@EnableAutoConfiguration(exclude={DataSource ...
- Ubuntu系统---C++之VScode IDE 编译器安装
Ubuntu系统---C++之VScode IDE 编译器安装 简单了解了一下VScode,直观印象:安装包很小(不像VS那么大占用十G左右).跨平台.小巧.可以编译C++ / java / pyth ...
- python c++ 混合编程中python调用c++string返回类型的函数,python中返回为数字的解决办法
本随笔解决 Python使用ctypes 调用c++dll 字符串返回类型函数,在python中显示为数字:原文解决方案见so: https://stackoverflow.com/questions ...