First thing I wan't to clarify is, I know Servlet is single instance multi-threaded model. Container maintains on instance and creates multiple threads per multiple requests to handle and all the threads work on same servlet instance. Also, we shouldn't maintain state in a servlet as it causes multi-threading/concurrency issues. Cool!! What if the state (member's of servlet) itself is thread-safe?? As in below example
public class MyCrazyServlet extends HttpServlet {
StringBuffer s1 = new StringBuffer("stackoverflow");
static StringBuffer s2 = new StringBuffer("stackexchange");
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String s3 = "I'm safe";
HttpSession crazySession = request.getSession();
//more stuff here
}
}
In above snippet, I presume except crazySession local variable (HttpSession instance) everything else (s1, s2 and s3) is thread safe
Please correct, if my understanding is wrong