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());
}
}
}