public class MyServlet extends HttpServlet{ private static final long serialVersionUID = 1L; private String userName1 = null;//实例变量 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ userName1 = req.getParameter("userName1"); String userName2 = req.getParameter("userName2");//局部变量 //TODO 其他处理 } }
public class MyServlet extends HttpServlet{ private static final long serialVersionUID = 1L; private String userName1 = null;//实例变量 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ synchronized (userName1) { userName1 = req.getParameter("userName1"); //TODO } String userName2 = req.getParameter("userName2");//局部变量 //TODO 其他处理 } }
java servlet拾遗(3)-servlet 线程安全问题
原文:http://haoran-10.iteye.com/blog/2245065