004.Servet种多线程不安全及解决方案
package com.lll.servlet; import javax.servlet.*; import java.io.IOException; public class ServletDemo2 implements Servlet { public String str="A"; @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("访问到了INIT方法!!!");//创建 } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println(str); this.str="B"; System.out.println(str); System.out.println("访问到了SERVLET2方法!!!");//服务 } @Override public void destroy() { System.out.println("访问到了DESTORY方法!!!");//销毁 } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } }
用第一个浏览器打开:
用第二个浏览器打开:
解决方案: