servlet的單例模式
servlet 在容器中是單例模式存在的,只會創建一個.
每次用戶發送了請求到容器,容器就會從線程池中分配一個線程給這個請求,請求完畢之后線程將會再次回收到池中,
多個線程同時訪問一個 servlet, 就會存在安全風險
Demo: 避免 servlet 安全風險
第一種方式: 創建單線程的 servlet (使用servlet 的單線程創建模式)
之后 servlet, 就同一時間只能一個線程訪問 (一個用戶訪問),
其他用戶需要排隊訪問 servlet, 但這種方式不能使用
1 @SuppressWarnings("serial") 2 public class EmpServlet extends HttpServlet implements SingleThreadModel{ 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 try { 6 TimeUnit.SECONDS.sleep(10); 7 } catch (InterruptedException e) { 8 e.printStackTrace(); 9 } 10 System.out.println(Thread.currentThread().getName() + "線程正在使用 servlet"); 11 } 12 }
第二種方式:在方法中使用同步代碼塊
和第一種方式一樣,也不能使用
1 @SuppressWarnings("serial") 2 public class EmpServlet extends HttpServlet{ 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 synchronized (this) { 6 System.out.println(Thread.currentThread().getName() + "線程正在使用 servlet"); 7 try { 8 TimeUnit.SECONDS.sleep(5); 9 } catch (InterruptedException e) { 10 e.printStackTrace(); 11 } 12 } 13 } 14 }
第三種方式: 避開 servlet 中定義實例變量
如果需要使用到變量就在方法中定義為局部變量,此時就不存在數據共享問題
總結:
第三種方案是推薦的, 第一二種方案會導致訪問的速度風場慢, 需要線程同步(排隊等待)