Servlet线程安全问题:
代码示例:
package com.bjpowernode.javaweb.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class RegisterServlet extends HttpServlet { private static final long serialVersionUID = 1L; //实例变量
private String username; @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("UTF-8"); //局部变量
//String username = request.getParameter("username"); synchronized(this){
//实例变量
username = request.getParameter("username");
out.print("恭喜你" + username + "注册成功");
}
} }
路径编写总结: