Servlet线程安全问题:
代码示例:
1 package com.bjpowernode.javaweb.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class RegisterServlet extends HttpServlet { 12 13 private static final long serialVersionUID = 1L; 14 15 //实例变量 16 private String username; 17 18 @Override 19 protected void doPost(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 22 response.setContentType("text/html;charset=UTF-8"); 23 PrintWriter out = response.getWriter(); 24 request.setCharacterEncoding("UTF-8"); 25 26 //局部变量 27 //String username = request.getParameter("username"); 28 29 synchronized(this){ 30 //实例变量 31 username = request.getParameter("username"); 32 out.print("恭喜你" + username + "注册成功"); 33 } 34 } 35 36 37 }
路径编写总结: